C 11多執行緒學習筆記(一)

2022-02-15 15:50:12 字數 3732 閱讀 6895

程序(英語:process),是指計算機中已執行的程式。程序曾經是分時系統的基本運作單位。

執行緒(英語:thread)是作業系統能夠進行運算排程的最小單位。大部分情況下,它被包含在程序之中,是程序中的實際運作單位。一條執行緒指的是程序中乙個單一順序的控制流,乙個程序中可以併發多個執行緒,每條執行緒並行執行不同的任務。

以上摘自wiki

\(std::thread定義於標頭檔案 \)

#include #include #include #include void f1(int n)} 

void f2(int& n)}

class foo

}int n = 0;};

class baz

}int n = 0;};

int main()

)// 通過lambda表示式建立

t2.join();

t4.join();

t5.join();

t6.join();

std::cout << "final value of n is " << n << '\n';

std::cout << "final value of foo::n is " << f.n << '\n';

std::cout << "final value of baz::n is " << b.n << '\n';}/*

thread 1 executing

thread 2 executing

thread 3 executing

thread 4 executing

thread 3 executing

thread 1 executing

thread 2 executing

thread 4 executing

thread 2 executing

thread 3 executing

thread 1 executing

thread 4 executing

thread 3 executing

thread 2 executing

thread 1 executing

thread 4 executing

thread 3 executing

thread 1 executing

thread 2 executing

thread 4 executing

final value of n is 5

final value of foo::n is 5

final value of baz::n is 5

*/

以上摘自cppreference

#include #include #include using namespace std;

class factor

};int main()

;//使用std::ref函式傳遞引用, 這裡定義乙個預設建構函式factor,並呼叫過載運算子()函式

t1.join();

cout << str << endl;

return 0;

}

get_id()返回執行緒的id

#include #include using namespace std;

int main()

); cout << "執行緒id為:" << t1.get_id() << endl;

t1.join();

return 0;}/*

執行緒id為:2

執行緒建立成功!

*/

阻塞當前程序,直至join所指程序執行完畢,也就是只有等join裡面的程序執行結束後才能執行join後面的語句

未加join阻塞執行緒

#include #include using namespace std;

void func()

int main()

/*永遠嘀神!

永遠嘀神!

1永遠嘀神!

永遠嘀神!2

永遠嘀神!345

*/

由執行結果可看出func(子執行緒)main(主線程)同時執行

加了join之後

#include #include using namespace std;

void func()

int main()/*1

2345

永遠嘀神!

永遠嘀神!

永遠嘀神!

永遠嘀神!

永遠嘀神!

*/

主線程等待子執行緒執行完再執行

再看乙個例子

#include #include using namespace std;

void func()

int main()

/*永遠嘀神!

terminate called without an active exception

*/

子執行緒在主線程結束是被強制終止。

​ 被detach後的執行緒成為守護執行緒或者分離執行緒,該執行緒脫離主線程的管制,成為獨立的乙個執行緒,執行完後由作業系統**

#include #include using namespace std;

void func()

int main()

/*永遠嘀神!

*/

判斷執行緒是否結束

#include #include #include void foo()

int main()

/*before starting, joinable: false

after starting, joinable: true

after joining, joinable: false

*/

交換兩個thread的內容

#include #include #include void foo()

void bar()

int main()

/*thread 1 id: 2

thread 2 id: 3

thread 1 address: 0x6dfedc

thread 2 address: 0x6dfed8

after std::swap(t1, t2):

thread 1 id: 3

thread 2 id: 2

thread 1 address: 0x6dfedc

thread 2 address: 0x6dfed8

after t1.swap(t2):

thread 1 id: 2

thread 2 id: 3

thread 1 address: 0x6dfedc

thread 2 address: 0x6dfed8

*/

c 11多執行緒課堂筆記

4 諸如 int a a 1 char a a a 這種寫法是錯的,空位址不能賦值 5 char aa char asd 這是可以的 6 student s4 s3 呼叫的是拷貝構造!並非拷貝賦值,因為這是建立物件 7 student s4 s3 s4 s1 後面那個才是呼叫拷貝賦值,拷貝賦值是對已...

C 11 多執行緒

新特性之描述 雖然 c 11 會在語言的定義上提供乙個記憶體模型以支援執行緒,但執行緒的使用主要將以 c 11 標準庫的方式呈現。c 11 標準庫會提供型別 thread std thread 若要執行乙個執行緒,可以建立乙個型別 thread 的實體,其初始引數為乙個函式物件,以及該函式物件所需要...

c 11 多執行緒

1.多執行緒的原理 同一時間內,cpu只能處理1條執行緒,只有1條執行緒在工作 執行 多執行緒併發 同時 執行,其實是cpu快速地在多條執行緒之間排程 切換 如果cpu排程執行緒的時間足夠快,就造成了多執行緒併發執行的假象。思考 如果執行緒非常非常多,會發生什麼情況?cpu會在n多執行緒之間排程,c...