C 併發 轉移執行緒的所有權

2021-09-26 21:04:42 字數 504 閱讀 9285

void function();

void function1();

std::thread t1( function );   //啟動乙個新執行緒t1與function關聯

std::thread t2 = std::move(t1); //將t1限行的轉移給t2

t1 = std::thread( function1 ); //啟動乙個臨時的物件與function1關聯

std::thread t3; //預設構造乙個t3,無任何關聯

t3 = std::move(t2);//將t2顯性的轉移給t3,此刻t2無任何關聯

t1 = std::move(t3);//因為t1已經有乙個關聯,此時會呼叫std::terminate()來終止程式

std::thread還支援return 來轉移

std::thread g()

void t1(int i);

std::thread f(t1,40);

return f;

c 轉移執行緒所有權

c 中有很多資源占有型別,比如std ifstream,std unique ptr,std thread 都是可以移動不可拷貝的。可以通過std move 轉移乙個執行緒所有權給乙個未關聯執行緒的thread物件,但是不能通過賦乙個新值的方式丟棄乙個執行緒。void haha void heihe...

c 11併發程式設計歷程(5)轉移執行緒所有權

include include class scoped thread intstart std thread t scoped thread scoped thread scoped thread const delete scoped thread operator scoped thread ...

Qt 所有權問題導致多執行緒報錯

問題描述,把定時器放在單獨執行緒中使用,想提高精度。結果報錯 qobject starttimer timers cannot be started from another thread。這種報錯方式,以前剛用多執行緒時候遇見過。知道原因,所以更疑惑,百思不得其解。在q群問,集思廣益。1.多執行緒...