久久久久久久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)行排序)
主站蜘蛛池模板: 狠狠涩| 久久久久久成人 | 亚洲一区二区 | www国产亚洲精品久久网站 | 国产日韩免费视频 | 久久福利电影 | 精品国产乱码久久久久久蜜臀 | a中文在线视频 | 午夜丰满寂寞少妇精品 | 91精品国产乱码久久久久久久久 | 欧美视频一级 | av网址在线| 九九热在线免费视频 | 亚洲人成网站777色婷婷 | 成人欧美一区二区三区在线播放 | 欧美日韩视频在线 | 日韩喷潮| 日韩欧美在线观看视频 | 九九热免费观看 | 欧美日高清 | 国产91丝袜在线播放 | 欧美日韩不卡合集视频 | 一区在线观看 | 成人精品一区二区三区中文字幕 | 日本精品久久久久久久 | 亚洲av一级毛片 | 一本色道久久综合亚洲精品高清 | 国产视频1区2区 | 亚洲看片网站 | 成人网视频 | 久久精品亚洲精品国产欧美 | 天堂亚洲| 黄色欧美视频 | 一区二区精品 | 涩爱av一区二区三区 | 国产成人精品在线播放 | 国产91丝袜在线播放 | 成人精品国产 | 欧美色专区 | 91大神在线看 | 九一在线 |