簡介: CSDN博客專家,專注Android/Linux系統(tǒng),分享多mic語音方案、音視頻、編解碼等技術(shù),與大家一起成長!
優(yōu)質(zhì)專欄:Audio工程師進(jìn)階系列【原創(chuàng)干貨持續(xù)更新中……】??
人生格言: 人生從來沒有捷徑,只有行動才是治療恐懼和懶惰的唯一良藥.
1.前言
本篇目的:介紹C++11 thread線程用法
2.C++11 thread創(chuàng)建傳參線程
<1>. t1.join(): 主進(jìn)程等待任務(wù)線程示例
#include <iostream>
#include <thread>
#include <unistd.h>
using namespace std;
void task_01(){
printf("xxx------->%s(), line = %d\n",__FUNCTION__,__LINE__);
}
void task_02(int i){
printf("xxx------->%s(), line = %d\n",__FUNCTION__,__LINE__);
while(i < 1000)
i++;
printf("xxx------->%s(), line = %d, i = %d\n",__FUNCTION__,__LINE__,i);
}
int main(){
int i = 0, j = 0;
//1.任務(wù)線程不帶參數(shù)
thread t1(task_01);
//2.任務(wù)線程帶參數(shù)
thread t2(task_02, i);
//t1.detach();//線程與進(jìn)程分離,各自執(zhí)行,順序是混換的.
t1.join();//主進(jìn)程等待線程執(zhí)行結(jié)束,然后主進(jìn)程開始執(zhí)行.
printf("xxx------->%s(), line = %d\n",__FUNCTION__,__LINE__);
return 0;
}
打印:
xxx------->task_01(), line = 7
xxx------->task_01(), line = 10, i = 1000
xxx------->main(), line = 20
在thread中創(chuàng)建一個任務(wù)線程task_01(),并將參數(shù)i = 1傳進(jìn)去; 然后 t1.join();
通過打印看出,主進(jìn)程會等待線程執(zhí)行完畢,才開始執(zhí)行.
<2>. t1.detach(): 主進(jìn)程和任務(wù)線程分離示例
#include <iostream>
#include <thread>
#include <unistd.h>
using namespace std;
void task_01(int i){
printf("xxx------->%s(), line = %d\n",__FUNCTION__,__LINE__);
while(i < 1000)
i++;
printf("xxx------->%s(), line = %d, i = %d\n",__FUNCTION__,__LINE__,i);
}
int main(){
int i = 0, j = 0;
//1.任務(wù)線程.
thread t1(task_01, i);
t1.detach();//線程與進(jìn)程分離,各自執(zhí)行,順序是混換的.
//t1.join();//主進(jìn)程等待線程執(zhí)行結(jié)束,然后主進(jìn)程開始執(zhí)行.
printf("xxx------->%s(), line = %d\n",__FUNCTION__,__LINE__);
return 0;
}
打印
xxx------->main(), line = 20文章來源:http://www.zghlxwxcb.cn/news/detail-414416.html
在thread中創(chuàng)建一個任務(wù)線程task_01(),并將參數(shù)i = 1傳進(jìn)去; 然后 t1.detach();
通過打印看出,主進(jìn)程和任務(wù)線程各自執(zhí)行,結(jié)果是主進(jìn)程執(zhí)行完了,任務(wù)線程沒來的及執(zhí)行;
也有可能,任務(wù)線程執(zhí)行完了,主進(jìn)程還沒來得及執(zhí)行,這種情況都有可能.文章來源地址http://www.zghlxwxcb.cn/news/detail-414416.html
<3>.線程傳遞對象引用
#include<iostream>
#include<thread>
using namespace std;
class A {
public:
int num;
A(){};
};
void test(A& a) {
a.num = 199;
cout << "a.num = " << a.num << endl;
}
int main() {
A a;
a.num = 10;
thread th(test, std::ref(a));
th.join();
cout << "a.num = " << a.num << endl;
return 0;
}
<4>.線程傳遞指針對象
#include<iostream>
#include<thread>
using namespace std;
class A {
public:
int num;
A(){};
};
void test(A* a) {
a->num = 199;
cout << "a.num = " << a->num << endl;
}
int main() {
A *a = new A();
a->num = 10;
//thread th(test, std::ref(a));
thread th(test, a);
th.join();
cout << "a.num = " << a->num << endl;
return 0;
}
<5>.線程傳遞this指針
#include<iostream>
#include<thread>
using namespace std;
class A {
public:
int num;
A(){};
void print();
};
void test(A* a) {
a->num = 199;
cout << "a.num = " << a->num << endl;
}
void A::print(){
thread th(test, this);
th.join();
cout << "a.num = " << this->num << endl;
}
int main() {
A *a = new A();
a->num = 10;
a->print();
return 0;
}
<6>.thread線程lambda表達(dá)式用法
/***********************************************************
* Author : 公眾號: Android系統(tǒng)攻城獅
* Create time : 2023-07-28 22:06:42 星期五
* Filename : thread_lambda_05.cpp
* Description :
************************************************************/
#include <iostream>
#include <thread>
#include <chrono>
#include <atomic>
#include <mutex>
#include <thread>
int main(){
std::mutex mutex;
std::thread thread = std::thread([&]() {
std::lock_guard<decltype(mutex)> lock(mutex);
for (int i=10; i>0; --i) {
std::cout << i << std::endl;
std::this_thread::sleep_for (std::chrono::seconds(1));
}
});
thread.join();
return 0;
}
到了這里,關(guān)于C++之C++11 thread線程示例(一百三十八)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!