欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c++11&14-多線程要點匯總

 更新時間:2020年06月02日 16:44:20   作者:晟夏的葉  
這篇文章主要介紹了c++11&14-多線程的使用方法,文中代碼非常詳細,方便大家更好的參考和學習,感興趣的朋友快來了解下

在C++11以前,C++的多線程編程均需依賴系統(tǒng)或第三方接口實現(xiàn),一定程度上影響了代碼的移植性。C++11中,引入了boost庫中的多線程部分內(nèi)容,形成C++標準,形成標準后的boost多線程編程部分接口基本沒有變化,這樣方便了以前使用boost接口開發(fā)的使用者切換使用C++標準接口,很容易把boost接口升級為C++標準接口。

我們通過如下幾部分介紹C++11多線程方面的接口及使用方法。

1. std::thread

std::thread為C++11的線程類,使用方法和boost接口一樣,非常方便,同時,C++11的std::thread解決了boost::thread中構成參數(shù)限制的問題,我想這都是得益于C++11的可變參數(shù)的設計風格。

我們通過如下代碼熟悉下std::thread使用風格:

//c11.cpp
#include <iostream>
#include <thread>
void threadfun1()
{
 std::cout << "threadfun1 - 1\r\n" << std::endl;
 std::this_thread::sleep_for(std::chrono::seconds(1));
 std::cout << "threadfun1 - 2" << std::endl;
}
void threadfun2(int iParam, std::string sParam)
{
 std::cout << "threadfun2 - 1" << std::endl;
 std::this_thread::sleep_for(std::chrono::seconds(5));
 std::cout << "threadfun2 - 2" << std::endl;
}
int main()
{
 std::thread t1(threadfun1);
 std::thread t2(threadfun2, 10, "abc");
 t1.join();
 std::cout << "join" << std::endl;
 t2.detach();
 std::cout << "detach" << std::endl;
}

注意編譯時要使用:g++ c11.cpp -lpthread

運行結果:

threadfun1 - 1
threadfun2 - 1
threadfun1 - 2
join
detach

2. std::atomic

std::atomic為C++11封裝的原子數(shù)據(jù)類型。
什么是原子數(shù)據(jù)類型?從功能上看,簡單地說,原子數(shù)據(jù)類型不會發(fā)生數(shù)據(jù)競爭,能直接用在多線程中而不必我們用戶對其進行添加互斥資源鎖的類型。從實現(xiàn)上來看,我們可以理解為這些原子類型內(nèi)部自己加了鎖。

我們下面通過一個測試例子說明原子類型std::atomic的特點。

我們使用10個線程,把std::atomic類型的變量iCount從10減到1。

//c11.cpp
#include <thread>
#include <atomic>
#include <stdio.h>
#include <iostream>
#include <list>
std::atomic<bool> bIsReady(false);
std::atomic<int> iCount(10);
void threadfun1()
{
 if (!bIsReady) {
  std::this_thread::yield();
 }
 while (iCount > 0)
 {
  printf("iCount:%d\r\n", iCount--);
 }
}
int main()
{
 std::list<std::thread> lstThread;
 for (int i = 0; i < 10; ++i)
 {
  lstThread.push_back(std::thread(threadfun1));
 }
 for (auto& th : lstThread)
 {
  th.join();
 }
}

運行結果:

iCount:10
iCount:9
iCount:8
iCount:7
iCount:6
iCount:5
iCount:4
iCount:3
iCount:2
iCount:1

從上面的結果可以看到,iCount的最小結果是1,沒有出現(xiàn)小于等于0的情況,大家可以把iCount改成100甚至1000看看,可能會更直觀一點。

3. std::condition_variable

C++11中的std::condition_variable就像Linux下使用pthread_cond_wait和pthread_cond_signal一樣,可以讓線程休眠,直到被喚醒,然后再重新執(zhí)行。線程等待在多線程編程中使用非常頻繁,經(jīng)常需要等待一些異步執(zhí)行的條件的返回結果。

代碼如下:

#include <iostream> // std::cout
#include <thread> // std::thread
#include <mutex> // std::mutex, std::unique_lock
#include <condition_variable> // std::condition_variable
std::mutex mtx;
std::condition_variable cv;
bool ready = false;
void print_id(int id) {
 std::unique_lock<std::mutex> lck(mtx);
 while (!ready) cv.wait(lck); //線程將進入休眠
 // ...
 std::cout << "thread " << id << '\n';
}
void go() {
 std::unique_lock<std::mutex> lck(mtx);
 ready = true;
 cv.notify_all();
}
int main()
{
 std::thread threads[10];
 // spawn 10 threads:
 for (int i = 0; i<10; ++i)
  threads[i] = std::thread(print_id, i);
 std::cout << "10 threads ready to race...\n";
 go(); // go!
 for (auto& th : threads) th.join();
 return 0;
}

運行結果:

10 threads ready to race...
thread 0
thread 1
thread 2
thread 3
thread 4
thread 5
thread 6
thread 7
thread 8
thread 9

上面的代碼,在調用go函數(shù)之前,10個線程都處于休眠狀態(tài),當cv.notify_all()運行后,線程休眠結束,繼續(xù)往下運行,最終輸出如上結果。

以上就是c++11&14-多線程知識匯總的詳細內(nèi)容,更多關于c++11&14 多線程使用的資料請關注腳本之家其它相關文章!

相關文章

  • C++實現(xiàn)LeetCode(199.二叉樹的右側視圖)

    C++實現(xiàn)LeetCode(199.二叉樹的右側視圖)

    這篇文章主要介紹了C++實現(xiàn)LeetCode(199.二叉樹的右側視圖),本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • C語言實現(xiàn)飛機小游戲

    C語言實現(xiàn)飛機小游戲

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)飛機小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • C++與C語言常用的語法對比

    C++與C語言常用的語法對比

    這篇文章主要介紹了C++與C語言常用的語法對比,文章基于c++和C語言的相關資料展開兩者的語法相互對比,需要的小伙伴可以參考一下,希望對你的學習有所幫助
    2022-04-04
  • 詳解C++中mutable的用法

    詳解C++中mutable的用法

    這篇文章主要介紹了詳解C++中mutable的用法,幫助大家更好的理解和學習C++,感興趣的朋友可以了解下
    2020-08-08
  • Inline Hook(ring3)的簡單C++實現(xiàn)方法

    Inline Hook(ring3)的簡單C++實現(xiàn)方法

    這篇文章主要介紹了Inline Hook(ring3)的簡單C++實現(xiàn)方法,需要的朋友可以參考下
    2014-08-08
  • C++模板編程特性之移動語義

    C++模板編程特性之移動語義

    首先,移動語義和完美轉發(fā)這兩個概念是在C++的模板編程的基礎上,新增的特性,主要是配合模板來使用。本篇會從C++的值類型,到移動拷貝與移動賦值來理解移動語義與完美轉發(fā)
    2022-08-08
  • 從頭學習C語言之for語句和循環(huán)嵌套

    從頭學習C語言之for語句和循環(huán)嵌套

    這篇文章主要為大家詳細介紹了C語言之for語句和循環(huán)嵌套,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01
  • C語言輸出旋轉后數(shù)組中的最小數(shù)元素的算法原理與實例

    C語言輸出旋轉后數(shù)組中的最小數(shù)元素的算法原理與實例

    這篇文章主要介紹了C語言輸出旋轉后數(shù)組中的最小數(shù)元素的算法原理與實例,數(shù)組旋轉就是把開頭的幾個指定的元素放到數(shù)組的末尾,需要的朋友可以參考下
    2016-03-03
  • 詳解C++中的數(shù)據(jù)抽象

    詳解C++中的數(shù)據(jù)抽象

    這篇文章主要介紹了詳解C++中的數(shù)據(jù)抽象,數(shù)據(jù)抽象是指,只向外界提供關鍵信息,并隱藏其后臺的實現(xiàn)細節(jié),即只表現(xiàn)必要的信息而不呈現(xiàn)細節(jié),需要的朋友可以參考下
    2023-05-05
  • C++實現(xiàn)LeetCode(33.在旋轉有序數(shù)組中搜索)

    C++實現(xiàn)LeetCode(33.在旋轉有序數(shù)組中搜索)

    這篇文章主要介紹了C++實現(xiàn)LeetCode(33.在旋轉有序數(shù)組中搜索),本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-07-07

最新評論