免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版
打開APP
未登錄
開通VIP,暢享免費電子書等14項超值服
開通VIP
首頁
好書
留言交流
下載APP
聯(lián)系客服
C++之多線程(C++11 thread.h文件實現(xiàn)多線程)
禁忌石
>《多線程》
2017.05.30
關(guān)注
C++之多線程(C++11 thread.h文件實現(xiàn)多線程)
標簽:
c++11多線程
thread
2016-06-14 21:30 6344人閱讀
評論
(2)
舉報
分類:
C++基礎(chǔ)(38)
轉(zhuǎn)載自:
http://www.cnblogs.com/haippy/p/3235560.html
http://www.cnblogs.com/lidabo/p/3908705.html
與
C++
11 多線程相關(guān)的頭文件
C++11 新標準中引入了四個頭文件來支持多線程編程,他們分別是<atomic> ,<thread>,<mutex>,<condition_variable>和<future>。
<atomic>:該頭文主要聲明了兩個類, std::atomic 和 std::atomic_flag,另外還聲明了一套 C 風格的原子類型和與 C 兼容的原子操作的函數(shù)。
<thread>:該頭文件主要聲明了 std::thread 類,另外 std::this_thread 命名空間也在該頭文件中。
<mutex>:該頭文件主要聲明了與互斥量(mutex)相關(guān)的類,包括 std::mutex 系列類,std::lock_guard, std::unique_lock, 以及其他的類型和函數(shù)。
<condition_variable>:該頭文件主要聲明了與條件變量相關(guān)的類,包括 std::condition_variable 和 std::condition_variable_any。
<future>:該頭文件主要聲明了 std::promise, std::package_task 兩個 Provider 類,以及 std::future 和 std::shared_future 兩個 Future 類,另外還有一些與之相關(guān)的類型和函數(shù),std::async() 函數(shù)就聲明在此頭文件中。
demo1:thread "Hello world"
[cpp]
view plain
copy
#include <stdio.h>
#include <stdlib.h>
#include <iostream> // std::cout
#include <thread> // std::thread
void thread_task() {
std::cout << "hello thread" << std::endl;
}
int main(int argc, const char *argv[])
{
std::thread t(thread_task);
t.join();//和主線程協(xié)同
return EXIT_SUCCESS;
}
demo2:一次啟動多個線程
我們通常希望一次啟動多個線程,來并行工作。為此,我們可以創(chuàng)建線程組,而不是在先前的舉例中那樣創(chuàng)建一條線程。下面的例子中,主函數(shù)創(chuàng)建十條為一組的線程,并且等待這些線程完成他們的任務(wù)
[cpp]
view plain
copy
#include <stdio.h>
#include <stdlib.h>
#include <iostream> // std::cout
#include <thread> // std::thread
int main() {
std::thread t[num_threads];
//Launch a group of threads 啟動一組線程
for (int i = 0; i < num_threads; ++i) {
t[i] = std::thread(call_from_thread);
}
std::cout << "Launched from the mainn";
//Join the threads with the main thread
for (int i = 0; i < num_threads; ++i) {
t[i].join();
}
return 0;
}
記住,主函數(shù)也是一條線程,通常叫做主線程,所以上面的代碼實際上有11條線程在運行。在啟動這些線程組之后,線程組和主函數(shù)進行協(xié)同(join)之前,允許我們在主線程中做些其他的事情。
demo3:在線程中使用帶有形參的函數(shù)
[cpp]
view plain
copy
#include <stdio.h>
#include <stdlib.h>
#include <iostream> // std::cout
#include <thread> // std::thread
static const int num_threads = 10;
//This function will be called from a thread
void call_from_thread(int tid) {
std::cout << "Launched by thread " << tid << std::endl;
}
int main() {
std::thread t[num_threads];
//Launch a group of threads
for (int i = 0; i < num_threads; ++i) {
t[i] = std::thread(call_from_thread, i);
}
std::cout << "Launched from the mainn";
//Join the threads with the main thread
for (int i = 0; i < num_threads; ++i) {
t[i].join();
}
return 0;
}
運行結(jié)果:
[plain]
view plain
copy
Sol$ ./a.out
Launched by thread 0
Launched by thread 1
Launched by thread 2
Launched from the main
Launched by thread 3
Launched by thread 5
Launched by thread 6
Launched by thread 7
Launched by thread Launched by thread 4
8L
aunched by thread 9
Sol$
能看到上面的結(jié)果中,程序一旦創(chuàng)建一條線程,其運行存在先后秩序不確定的現(xiàn)象。程序員的任務(wù)就是要確保這組線程在訪問公共數(shù)據(jù)時不要出現(xiàn)阻塞。最后幾行,所顯示的錯亂輸出,表明8號線程啟動的時候,4號線程還沒有完成在stdout上的寫操作。事實上假定在你自己的機器上運行上面的代碼,將會獲得全然不同的結(jié)果,甚至是會輸出些混亂的字符。原因在于,程序內(nèi)的11條線程都在競爭性地使用stdout這個公共資源(案:Race Conditions)。
要避免上面的問題,可以在代碼中使用攔截器(barriers),如std:mutex,以同步(synchronize)的方式來使得一群線程訪問公共資源,或者,如果可行的話,為線程們預留下私用的數(shù)據(jù)結(jié)構(gòu),避免使用公共資源。我們在以后的教學中,還會講到線程同步問題,包括使用原子操作類型(atomic types)和互斥體(mutex)。
更多內(nèi)容請參考:
C++11 并發(fā)指南一(C++11 多線程初探)
http://www.cnblogs.com/haippy/p/3235560.html
C++11 并發(fā)指南二(std::thread 詳解)
http://www.cnblogs.com/haippy/p/3236136.html
C++11 并發(fā)指南三(std::mutex 詳解)http://www.cnblogs.com/haippy/p/3237213.html
C++11 并發(fā)指南三(Lock 詳解)http://www.cnblogs.com/haippy/p/3346477.html
C++11 并發(fā)指南四(<future> 詳解一 std::promise 介紹)http://www.cnblogs.com/haippy/p/3239248.html
C++11 并發(fā)指南四(<future> 詳解二 std::packaged_task 介紹)http://www.cnblogs.com/haippy/p/3279565.html
C++11 并發(fā)指南四(<future> 詳解三 std::future & std::shared_future)http://www.cnblogs.com/haippy/p/3280643.html
C++11 并發(fā)指南五(std::condition_variable 詳解)http://www.cnblogs.com/haippy/p/3252041.html
C++11 并發(fā)指南六(atomic 類型詳解一 atomic_flag 介紹)http://www.cnblogs.com/haippy/p/3252056.html
C++11 并發(fā)指南六( <atomic> 類型詳解二 std::atomic )http://www.cnblogs.com/haippy/p/3301408.html
C++11 并發(fā)指南六(atomic 類型詳解三 std::atomic (續(xù)))http://www.cnblogs.com/haippy/p/3304556.html
C++11 并發(fā)指南六(atomic 類型詳解四 C 風格原子操作介紹)http://www.cnblogs.com/haippy/p/3306625.html
C++11 并發(fā)指南七(C++11 內(nèi)存模型一:介紹)http://www.cnblogs.com/haippy/p/3412858.html
C++11 并發(fā)指南九(綜合運用: C++11 多線程下生產(chǎn)者消費者模型詳解)http://www.cnblogs.com/haippy/p/3252092.html
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點擊舉報
。
打開APP,閱讀全文并永久保存
查看更多類似文章
猜你喜歡
類似文章
C++使用thread類多線程編程
C++11 并發(fā)指南五(std::condition
C++11 并發(fā)指南三(std::mutex 詳解)
c++11線程池實現(xiàn)
boost::thread線程創(chuàng)建方式總結(jié)
C++實現(xiàn)線程同步的幾種方式
更多類似文章 >>
生活服務(wù)
首頁
萬象
文化
人生
生活
健康
教育
職場
理財
娛樂
藝術(shù)
上網(wǎng)
留言交流
回頂部
聯(lián)系我們
分享
收藏
點擊這里,查看已保存的文章
導長圖
關(guān)注
一鍵復制
下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!
聯(lián)系客服
微信登錄中...
請勿關(guān)閉此頁面
先別劃走!
送你5元優(yōu)惠券,購買VIP限時立減!
5
元
優(yōu)惠券
優(yōu)惠券還有
10:00
過期
馬上使用
×