久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

C++ 中線程的簡單示例

Simple example of threading in C++(C++ 中線程的簡單示例)
本文介紹了C++ 中線程的簡單示例的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

有人可以發(fā)布一個(gè)在 C++ 中啟動兩個(gè)(面向?qū)ο?線程的簡單示例.

Can someone post a simple example of starting two (Object Oriented) threads in C++.

我正在尋找可以擴(kuò)展運(yùn)行方法(或類似的東西)而不是調(diào)用 C 樣式線程庫的實(shí)際 C++ 線程對象.

I'm looking for actual C++ thread objects that I can extend run methods on (or something similar) as opposed to calling a C-style thread library.

我省略了任何特定于操作系統(tǒng)的請求,希望回復(fù)的人會回復(fù)要使用的跨平臺庫.我現(xiàn)在只是明確說明這一點(diǎn).

I left out any OS specific requests in the hopes that whoever replied would reply with cross platform libraries to use. I'm just making that explicit now.

推薦答案

創(chuàng)建一個(gè)你想讓線程執(zhí)行的函數(shù),例如:

Create a function that you want the thread to execute, eg:

void task1(std::string msg)
{
    std::cout << "task1 says: " << msg;
}

現(xiàn)在創(chuàng)建 thread 對象,它最終會像這樣調(diào)用上面的函數(shù):

Now create the thread object that will ultimately invoke the function above like so:

std::thread t1(task1, "Hello");

(您需要#include 才能訪問std::thread 類)

(You need to #include <thread> to access the std::thread class)

構(gòu)造函數(shù)的參數(shù)是線程將執(zhí)行的函數(shù),后跟函數(shù)的參數(shù).線程在構(gòu)建時(shí)自動啟動.

The constructor's arguments are the function the thread will execute, followed by the function's parameters. The thread is automatically started upon construction.

如果稍后您想等待線程完成執(zhí)行函數(shù),請調(diào)用:

If later on you want to wait for the thread to be done executing the function, call:

t1.join(); 

(加入意味著調(diào)用新線程的線程將等待新線程執(zhí)行完畢,然后它才會繼續(xù)自己的執(zhí)行).

(Joining means that the thread who invoked the new thread will wait for the new thread to finish execution, before it will continue its own execution).

#include <string>
#include <iostream>
#include <thread>

using namespace std;

// The function we want to execute on the new thread.
void task1(string msg)
{
    cout << "task1 says: " << msg;
}

int main()
{
    // Constructs the new thread and runs it. Does not block execution.
    thread t1(task1, "Hello");

    // Do other things...

    // Makes the main thread wait for the new thread to finish execution, therefore blocks its own execution.
    t1.join();
}

這里有關(guān)于 std::thread 的更多信息

  • 在 GCC 上,使用 -std=c++0x -pthread 編譯.
  • 這應(yīng)該適用于任何操作系統(tǒng),前提是您的編譯器支持此 (C++11) 功能.

這篇關(guān)于C++ 中線程的簡單示例的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

What is the fastest way to transpose a matrix in C++?(在 C++ 中轉(zhuǎn)置矩陣的最快方法是什么?)
Sorting zipped (locked) containers in C++ using boost or the STL(使用 boost 或 STL 在 C++ 中對壓縮(鎖定)容器進(jìn)行排序)
Rotating a point about another point (2D)(圍繞另一個(gè)點(diǎn)旋轉(zhuǎn)一個(gè)點(diǎn) (2D))
Image Processing: Algorithm Improvement for #39;Coca-Cola Can#39; Recognition(圖像處理:Coca-Cola Can 識別的算法改進(jìn))
How do I construct an ISO 8601 datetime in C++?(如何在 C++ 中構(gòu)建 ISO 8601 日期時(shí)間?)
Sort list using STL sort function(使用 STL 排序功能對列表進(jìn)行排序)
主站蜘蛛池模板: 日本大香伊一区二区三区 | 国产成人精品一区二区 | a级在线免费视频 | 精品国产一区二区在线 | 精品欧美视频 | 精品视频久久久 | 一区二区三区av | 国产激情一区二区三区 | 秋霞电影一区二区 | 国产高清精品一区二区三区 | 色姑娘av | 久久精品国产久精国产 | 中文字幕一区二区三区四区 | 日韩高清www| 午夜视频一区二区 | 久久国产精品一区二区三区 | 久久福利网站 | 一级做a爰片性色毛片 | 久久精品视频在线免费观看 | 欧美日韩三级 | 亚洲综合无码一区二区 | 日韩视频精品 | 91久久精品国产91久久性色tv | 亚洲欧美日韩精品久久亚洲区 | 91手机精品视频 | 欧美日本在线观看 | xnxx 日本免费 | 在线观看成人免费视频 | 美女午夜影院 | 一级特黄视频 | 日韩精品1区2区3区 国产精品国产成人国产三级 | 欧美中文字幕一区二区三区亚洲 | 午夜精品久久久久久久久久久久久 | 欧美久久久久久久 | 人人干人人干人人干 | 国产一区精品在线 | 日韩av网址在线观看 | 日韩中文字幕一区二区 | 91久久久久久 | 成人在线免费视频 | 国产欧美精品一区 |