日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C++中std::thread線程用法_C 語言

作者:hongwen_yul ? 更新時間: 2023-02-18 編程語言

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

欄目分類
最近更新