網站首頁 編程語言 正文
1:std::thread的基本用法
最簡單的 std::thread用法如下,調用 thread將立即同時開始執行這個新建立的線程,新線程的任務執行完畢之后, main()的主線程也會繼續執行。
#include<iostream>
#include<thread>
#include<windows.h>
#include<string>
using namespace std;
void myfunc_work() {
cout << "myfunc_work ....." << endl;
// do something 5s
Sleep(5000);
}
int main() {
std::thread t1(myfunc_work);
// 阻塞當前main主線程,待子線程執行完畢后,自己恢復主線程邏輯
t1.join();
cout << "main thread ....." << endl;
}
2:std:: thread常用的成員函數?
下面為C++? std::thread常用的成員函數
get_id()? ? 取得目前的線程 id, 回傳一個 std::thread::id? 類型
joinable()? ? 檢查是否可 join
join()? ?// 阻塞當前線程,等待子線程執行完畢
detach()? // 與該線程分離,一旦該線程執行完后它所分配的資源就會被釋放
native_handle()? ? 取得平臺原生的 native handle.
sleep_for()? ? // 停止目前線程一段指定的時間
yield()? ?// 暫時放棄CPU一段時間,讓給其他線程
void foo() {
cout << "foo\n";
}
void bar(int x) {
cout << "bar\n";
}
int main() {
//std::thread t1(myfunc_work);
//cout << "main thread ....." << endl;
阻塞當前main主線程,待子線程執行完畢后,自己恢復主線程邏輯
//t1.join();
thread t1(foo);
thread t2(bar, 10);
cout << "main,foo,bar execute concurrently....\n";
cout << "sleep 1s\n";
this_thread::sleep_for(chrono::seconds(2));
cout << "join t1\n";
t1.join();
cout << "join t2\n";
t2.join();
cout << "foo and bar thread complete";
}
很顯然:新線程建立后,是否立即執行新線程業務代碼,有一定的隨機性。
但是我們可以通過 thread.join()? 或者 sleep_for() 來控制代碼執行順序?
3:建立新 thread執行類別中的函數?
C++ std::thread 的構建可以傳入class類別中的成員函數,如下范例所示:AA::start 分別建立t1, t2 兩個線程,而 t1傳入 AA::a1 類別函數。
notice :?
? ? ?第一個參數:AA::a1 前面需要添加 &?
? ? ?第二個參數:代表的是那個類對象
? ? ?后面參數: 按照要求傳入即可
class AA
{
public:
void a1()
{
cout << "a1\n";
}
void a2(int n) {
cout << "a2 : " << n << "\n";
}
void start() {
thread t1(&AA::a1, this);
thread t2(&AA::a2, this,10);
t1.join();
t2.join();
}
private:
};
4:建立新 thread 執行 lambda expression?
std:: thread 的構建也可以傳入 lambda expression 表達式,如下范例:
5:join等待thread執行結束
在main主線程建立 t1線程后,主線程便繼續往下執行,如果主線程需要等待 t1執行完畢后才能繼續執行的話,就需要使用 join 。
等待 t1線程執行完 foo 后主線程才能繼續執行,如果 t1線程沒有執行完,主線程會一致阻塞在 join這一行。
void test2() {
cout << "foo begin...." << endl;
this_thread::sleep_for(chrono::milliseconds(5000));
cout << "foo end...." << endl;
}
int main() {
std::thread t1(test2);
cout << "main 1....." << endl;;
t1.join();
cout << "main 2.....";
cout << "main thread run over" << endl;
}
6:detach不等待 thread執行結束?
承上例:如果主線程不想等或者可以不用等待 t1線程,可以使用 detach來讓 t1線程分離,接著主線程就可以繼續執行,t1線程 也在繼續執行。
/**
join等待thread執行結束
*/
void test2() {
cout << "foo begin...." << endl;
this_thread::sleep_for(chrono::milliseconds(50));
cout << "foo end...." << endl;
}
int main() {
std::thread t1(test2);
cout << "main 1....." << endl;;
t1.detach();
cout << "main 2....."<< endl;
cout << "main thread run over" << endl;
Sleep(2000);
return 0;
}
7:std::thread 參數傳遞使用引用的方法
定義方法:
void? myFunc(int&? n) {
? ? ? ? std::cout << "myFunc? n = " << n << endl;
? ? ? ? n+= 10;
}
使用參數傳遞使用引用目的是: 希望建立另外一個線程去執行 myFunc , 之后需要取得這個 myFunc的運算結果,但是建立線程如果寫: std::thread t1(myFunc , n)? 這樣會編譯出錯。
因為在 std::thread 的參數傳遞方式為值傳遞,值傳遞是不可修改的左值,如果要讓其能修改,可以考慮通過 : std::ref 來達成。
void myFunc(int n) {
std::cout << "myFunc n = " << n << endl;
n += 10;
}
void myFunc_ref(int& n) {
std::cout << "myFunc reference n = " << n << endl;
n += 10;
}
int main() {
int n1 = 5;
thread t1(myFunc, n1);
t1.join();
cout << "main n1 = " << n1 << "\n";
int n2 = 10;
thread t2(myFunc_ref, std::ref(n2));
t2.join();
cout << "main n2 = " << n2 << "\n";
cout << "main thread run over" << endl;
return 0;
}
原文鏈接:https://blog.csdn.net/u013620306/article/details/128565614
相關推薦
- 2023-10-09 所有的引用類型都有自由可拓展性怎么理解
- 2023-03-11 Python基礎教程之增加和去除數字的千位分隔符_python
- 2022-11-13 git如何還原到某次commit并強制推送遠程_相關技巧
- 2022-04-01 kubelet請求觸發流控,導致節點NotReady
- 2023-03-16 numpy如何獲取array中數組元素的索引位置_python
- 2022-11-08 切換tab時,van-list中的onload事件沒觸發
- 2022-12-07 C語言如何求整數的位數及各位數字之和_C 語言
- 2023-01-07 anaconda?navigator打不開問題的解決方法_python
- 最近更新
-
- 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同步修改后的遠程分支