網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
Qt的封裝程度比較高的線(xiàn)程類(lèi)用多了,發(fā)現(xiàn)C++標(biāo)準(zhǔn)庫(kù)里面的線(xiàn)程庫(kù)有些生疏。這里總結(jié)一下C++標(biāo)準(zhǔn)庫(kù)里面的線(xiàn)程相關(guān)內(nèi)容,供大家參考使用。其實(shí)標(biāo)準(zhǔn)C++的線(xiàn)程庫(kù)也是挺好用的。
1.創(chuàng)建線(xiàn)程異步執(zhí)行
我們可以通過(guò)async函數(shù)直接異步創(chuàng)建一個(gè)線(xiàn)程,這種方法相對(duì)來(lái)說(shuō)比較簡(jiǎn)單,線(xiàn)程執(zhí)行的結(jié)果可以直接用future<T>來(lái)進(jìn)行獲取。
#include <iostream> #include <future> //線(xiàn)程對(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ò)使用互斥鎖防止線(xiàn)程沖突
線(xiàn)程間同步讀取內(nèi)容的話(huà)一般不會(huì)出現(xiàn)線(xiàn)程安全問(wèn)題,但如果線(xiàn)程間同步寫(xiě)同一個(gè)內(nèi)容的話(huà)就容易出現(xiàn)沖突。比如每個(gè)線(xiàn)程執(zhí)行一次,就會(huì)給全局執(zhí)行次數(shù)累加一次,如果多個(gè)線(xiàn)程同時(shí)執(zhí)行操作,在寫(xiě)的時(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)量控制線(xiàn)程的運(yùn)行
條件變量(condition_variable)用來(lái)控制線(xiàn)程的運(yùn)行,線(xiàn)程啟動(dòng)的時(shí)候如果條件變量等待,會(huì)阻塞線(xiàn)程的運(yùn)行,直到條件變量發(fā)送對(duì)應(yīng)的通知線(xiàn)程才能開(kāi)始運(yùn)行。通過(guò)采用條件變量我們可以控制線(xiàn)程的運(yùn)行,避免線(xià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í)候線(xiàn)程間執(zhí)行是有先后順序的,我們需要等待上一個(gè)線(xiàn)程執(zhí)行結(jié)束拿到結(jié)果之后再執(zhí)行當(dāng)前線(xiàn)程,這時(shí)候就涉及到線(xiàn)程間的等待和數(shù)據(jù)傳遞這時(shí)候std::promise<T>就能排上用場(chǎng)了,通過(guò)使用該變量我們可以很輕松的實(shí)現(xiàn)線(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)); //阻塞至線(xiàn)程結(jié)束 t1.join(); t2.join(); return 1; }
總結(jié)
原文鏈接:https://blog.csdn.net/yang1fei2/article/details/122795939
相關(guān)推薦
- 2023-01-21 python中封裝token問(wèn)題_python
- 2022-08-21 android實(shí)現(xiàn)貝塞爾曲線(xiàn)之波浪效果_Android
- 2022-05-17 Git 克隆指定分支的代碼
- 2022-08-01 Flask-Sqlalchemy的基本使用詳解_python
- 2022-10-06 Django數(shù)據(jù)映射(一對(duì)一,一對(duì)多,多對(duì)多)_python
- 2023-01-12 C語(yǔ)言中數(shù)組排序淺析_C 語(yǔ)言
- 2022-05-19 C++STL之string類(lèi)的使用_C 語(yǔ)言
- 2022-02-28 npm ERR! While resolving: undefined@undefined npm
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支