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

C++ 中線程的簡單示例

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

問題描述

有人可以發(fā)布一個在 C++ 中啟動兩個(面向對象)線程的簡單示例.

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

我正在尋找可以擴展運行方法(或類似的東西)而不是調用 C 樣式線程庫的實際 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)的請求,希望回復的人會回復要使用的跨平臺庫.我現(xià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)建一個你想讓線程執(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 對象,它最終會像這樣調用上面的函數(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)

構造函數(shù)的參數(shù)是線程將執(zhí)行的函數(shù),后跟函數(shù)的參數(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ù),請調用:

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

t1.join(); 

(加入意味著調用新線程的線程將等待新線程執(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();
}

這里有關于 std::thread 的更多信息

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

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

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

相關文檔推薦

What is the fastest way to transpose a matrix in C++?(在 C++ 中轉置矩陣的最快方法是什么?)
Sorting zipped (locked) containers in C++ using boost or the STL(使用 boost 或 STL 在 C++ 中對壓縮(鎖定)容器進行排序)
Rotating a point about another point (2D)(圍繞另一個點旋轉一個點 (2D))
Image Processing: Algorithm Improvement for #39;Coca-Cola Can#39; Recognition(圖像處理:Coca-Cola Can 識別的算法改進)
How do I construct an ISO 8601 datetime in C++?(如何在 C++ 中構建 ISO 8601 日期時間?)
Sort list using STL sort function(使用 STL 排序功能對列表進行排序)
主站蜘蛛池模板: 国产精品成人国产乱 | 日本在线免费观看 | 日本成人精品 | 激情久久久 | 黄色片一区二区 | 小sao货撅起屁股扒开c微博 | 亚洲午夜一区 | 日韩中文字幕第一页 | 小镇姑娘国语版在线观看免费 | 天堂网av在线 | 91视频亚洲| 伊人av综合 | 国产成人+综合亚洲+天堂 | av看片 | 国产午夜视频 | 午夜av在线播放 | 在线视频福利 | 毛片毛片毛片 | 欧美一级片免费看 | 好吊视频一区二区三区四区 | 国产精品va | 超碰福利在线 | 欧美一级在线视频 | 97精品国产97久久久久久免费 | 99国产精品99久久久久久 | 久久精品一区二区三区四区 | 三级av片 | 操欧美女人 | www久久久 | 伊人网av| 国产一区在线播放 | 国产一区二区欧美 | 欧美一级淫片免费视频魅影视频 | 亚洲另类色综合网站 | 欧美性生活网站 | 成人做爰9片免费视频 | 欧美在线观看一区 | 中文字幕日韩视频 | 免费欧美视频 | 国产成人精品亚洲 | 免费的av |