std thread 學習初步

2021-05-26 12:18:36 字數 1057 閱讀 5872

標準庫(c++0x)中的 thread 用起來似乎蠻簡單的。

乙個 std::thread 物件可以接收

作為引數。

#

include

#include

void

test1()

void

test2(const

qstring &text)

class

test3

void

operator()() const

};class

test4

void

operator()(const

qstring

b) const

};

class

test5

};class

test6

};

intmain(int

argc, char *argv)

, "test7

");return

a.exec();

}

std::thread t3((test3()));

std::thread t2(test2, "test 2");

要寫成

std::thread t2(std::bind(test2, "test 2"));

那乙個標準?

test3 test3;

test3();

std::thread t3(test3);

std::thread t33(std::ref(test3));

這三次呼叫的結果(類似於):

hello test3 0xbfc3b27f 

hello test3 0xbfc3b27f

hello test3 0x9811e60

std bind與std thread學習筆記

std bind 1 bind函式,如果在建立std bind將引數設定為具體變數,則在執行該bind函式時,這個引數的值就為設定時變數的值。並不會因為在呼叫bind的函式之前,使用的變數發生改變,而使用改變後的值。bind的那些不是佔位符的引數被拷貝到bind返回的可呼叫物件中 2 bind函式,...

std thread 傳遞引用引數

class myclass myclass myclass const myclass my void threadtest async void threadtest run const myclass arg 寫 的時候,擔心std thread傳遞引用時,會出現區域性變數先被釋放的情況。寫了個...

std thread 的併發測試

std thread 的併發測試,測試兩方面的效能 1 併發同步鎖的最優位置 2 併發最優執行緒數量 直接上 void ticket sell const int tickets for int i 0 i threadlst.size i std thread trd end end clock ...