C++中標(biāo)準(zhǔn)線程庫(kù)的基本使用介紹
Qt的封裝程度比較高的線程類用多了,發(fā)現(xiàn)C++標(biāo)準(zhǔn)庫(kù)里面的線程庫(kù)有些生疏。這里總結(jié)一下C++標(biāo)準(zhǔn)庫(kù)里面的線程相關(guān)內(nèi)容,供大家參考使用。其實(shí)標(biāo)準(zhǔn)C++的線程庫(kù)也是挺好用的。
1.創(chuàng)建線程異步執(zhí)行
我們可以通過(guò)async函數(shù)直接異步創(chuàng)建一個(gè)線程,這種方法相對(duì)來(lái)說(shuō)比較簡(jiǎn)單,線程執(zhí)行的結(jié)果可以直接用future<T>來(lái)進(jìn)行獲取。
#include <iostream>
#include <future>
//線程對(duì)應(yīng)的函數(shù)
bool thread_func(int x) {
return true;
}
int main()
{
int inputNum = 65547;
std::future<bool> future = std::async(thread_func, inputNum);
bool ret = future.get();
getchar();
}2.通過(guò)使用互斥鎖防止線程沖突
線程間同步讀取內(nèi)容的話一般不會(huì)出現(xiàn)線程安全問(wèn)題,但如果線程間同步寫同一個(gè)內(nèi)容的話就容易出現(xiàn)沖突。比如每個(gè)線程執(zhí)行一次,就會(huì)給全局執(zhí)行次數(shù)累加一次,如果多個(gè)線程同時(shí)執(zhí)行操作,在寫的時(shí)候沒(méi)有加鎖,這就有可能導(dǎo)致執(zhí)行次數(shù)被重復(fù)累加的情況。
#include <iostream>
#include <thread>
#include <mutex>
std::mutex mtx;
int count=0;
void print_block(int n) {
mtx.lock();
count++;
//do somethings
mtx.unlock();
}
int main()
{
std::thread thread1(print_block, 50);
std::thread thread2(print_block, 50);
thread1.join();
thread2.join();
getchar();
return 0;
}3.采用信號(hào)量控制線程的運(yùn)行
條件變量(condition_variable)用來(lái)控制線程的運(yùn)行,線程啟動(dòng)的時(shí)候如果條件變量等待,會(huì)阻塞線程的運(yùn)行,直到條件變量發(fā)送對(duì)應(yīng)的通知線程才能開(kāi)始運(yùn)行。通過(guò)采用條件變量我們可以控制線程的運(yùn)行,避免線程空運(yùn)行消耗計(jì)算資源。
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
std::mutex mtx;
std::condition_variable cv;
void print_id(int id) {
std::unique_lock<std::mutex> lck(mtx);
cv.wait(lck);
std::cout << "thread " << id << '\n';
}
void go() {
std::unique_lock<std::mutex> lck(mtx);
cv.notify_all();
}
int main()
{
std::thread threads[10];
for (int i = 0; i < 10; ++i)
threads[i] = std::thread(print_id, i);
std::cout << "start thread run" << std::endl;
go();
for (auto& th : threads){th.join();}
getchar();
return 0;
}4.通過(guò)promise實(shí)現(xiàn)進(jìn)程間通信
很多時(shí)候線程間執(zhí)行是有先后順序的,我們需要等待上一個(gè)線程執(zhí)行結(jié)束拿到結(jié)果之后再執(zhí)行當(dāng)前線程,這時(shí)候就涉及到線程間的等待和數(shù)據(jù)傳遞這時(shí)候std::promise<T>就能排上用場(chǎng)了,通過(guò)使用該變量我們可以很輕松的實(shí)現(xiàn)線程間的等待和數(shù)據(jù)傳遞。
#include <iostream>
#include <future>
#include <chrono>
void Thread_Fun1(std::promise<int> &p)
{
std::this_thread::sleep_for(std::chrono::seconds(5));
int iVal = 233;
std::cout << "傳入數(shù)據(jù)(int):" << iVal << std::endl;
p.set_value(iVal);
}
void Thread_Fun2(std::future<int> &f)
{
//阻塞函數(shù),直到收到相關(guān)聯(lián)的std::promise對(duì)象傳入的數(shù)據(jù)
auto iVal = f.get();
std::cout << "收到數(shù)據(jù)(int):" << iVal << std::endl;
}
int main()
{
std::promise<int> pr1;
std::future<int> fu1 = pr1.get_future();
std::thread t1(Thread_Fun1, std::ref(pr1));
std::thread t2(Thread_Fun2, std::ref(fu1));
//阻塞至線程結(jié)束
t1.join();
t2.join();
return 1;
}總結(jié)
到此這篇關(guān)于C++中標(biāo)準(zhǔn)線程庫(kù)的基本使用介紹的文章就介紹到這了,更多相關(guān)C++標(biāo)準(zhǔn)線程庫(kù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語(yǔ)言中socket相關(guān)網(wǎng)絡(luò)編程函數(shù)小結(jié)
這篇文章主要介紹了C語(yǔ)言中socket相關(guān)網(wǎng)絡(luò)編程函數(shù)小結(jié),是C語(yǔ)言入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09
一篇文章帶你了解C語(yǔ)言內(nèi)存對(duì)齊解決的問(wèn)題
內(nèi)存對(duì)齊的目的是為了提高CPU讀寫內(nèi)存里數(shù)據(jù)的速度。現(xiàn)代的CPU讀取內(nèi)存并不是一個(gè)一個(gè)字節(jié)挨著讀取,這樣做的效率非常低?,F(xiàn)代的CPU一般以4個(gè)字節(jié)(32bit數(shù)據(jù)總線)或者8個(gè)字節(jié)(64bit數(shù)據(jù)總線)為一組,一組一組地讀寫內(nèi)存里的數(shù)據(jù)2021-08-08
C++語(yǔ)言基礎(chǔ) this和static關(guān)鍵字
這篇文章主要介紹了C++語(yǔ)言基礎(chǔ) this和static關(guān)鍵字,需要的朋友可以參考下2020-01-01
C語(yǔ)言程序設(shè)計(jì)譚浩強(qiáng)第五版課后答案(第三章習(xí)題答案)
這篇文章主要介紹了C語(yǔ)言程序設(shè)計(jì)譚浩強(qiáng)第五版課后答案(第三章習(xí)題答案),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2021-04-04

