網站首頁 編程語言 正文
Qt的封裝程度比較高的線程類用多了,發現C++標準庫里面的線程庫有些生疏。這里總結一下C++標準庫里面的線程相關內容,供大家參考使用。其實標準C++的線程庫也是挺好用的。
1.創建線程異步執行
我們可以通過async函數直接異步創建一個線程,這種方法相對來說比較簡單,線程執行的結果可以直接用future<T>來進行獲取。
#include <iostream> #include <future> //線程對應的函數 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.通過使用互斥鎖防止線程沖突
線程間同步讀取內容的話一般不會出現線程安全問題,但如果線程間同步寫同一個內容的話就容易出現沖突。比如每個線程執行一次,就會給全局執行次數累加一次,如果多個線程同時執行操作,在寫的時候沒有加鎖,這就有可能導致執行次數被重復累加的情況。
#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.采用信號量控制線程的運行
條件變量(condition_variable)用來控制線程的運行,線程啟動的時候如果條件變量等待,會阻塞線程的運行,直到條件變量發送對應的通知線程才能開始運行。通過采用條件變量我們可以控制線程的運行,避免線程空運行消耗計算資源。
#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.通過promise實現進程間通信
很多時候線程間執行是有先后順序的,我們需要等待上一個線程執行結束拿到結果之后再執行當前線程,這時候就涉及到線程間的等待和數據傳遞這時候std::promise<T>就能排上用場了,通過使用該變量我們可以很輕松的實現線程間的等待和數據傳遞。
#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 << "傳入數據(int):" << iVal << std::endl; p.set_value(iVal); } void Thread_Fun2(std::future<int> &f) { //阻塞函數,直到收到相關聯的std::promise對象傳入的數據 auto iVal = f.get(); std::cout << "收到數據(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)); //阻塞至線程結束 t1.join(); t2.join(); return 1; }
總結
原文鏈接:https://blog.csdn.net/yang1fei2/article/details/122795939
相關推薦
- 2022-09-08 C++實現Dijkstra算法的示例代碼_C 語言
- 2022-06-21 Oracle新增和刪除用戶_oracle
- 2023-06-17 pytorch?簡介及常用工具包展示_python
- 2023-02-23 Golang中的錯誤處理深入分析_Golang
- 2022-05-19 python?字典常用方法超詳細梳理總結_python
- 2022-10-02 Android?Flutter實現搜索的三種方式詳解_Android
- 2022-08-11 boost.asio框架系列之buffer函數_C 語言
- 2022-10-27 go?熔斷原理分析與源碼解讀_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支