關於std::thread
有一點我不明白: 爲什麼std::thread
的構造函數需要函數運行rvalue?爲什麼std :: thread函數是由右值運行的?
我通常想與一些成員運行Functor到另一個線程。像這樣:
struct Function
{
void operator() (/* some args */)
{
/* some code */
}
/* some members */
}
void run_thread()
{
Functor f(/* some data */);
std::thread thread(f, /* some data */);
/* do something and wait for thread to finish */
}
當前實現std::thread
我必須確定我的對象是實現移動語義。我不明白爲什麼我不能通過引用來通過它。
額外的問題是:什麼意思是通過右值引用函數? Lambda表達式?
'std :: thread'的構造函數通過轉發引用來實現其功能。 –
你的對象不需要實現移動語義。如果移動不被支持,它只需要複製。 – nwp
@nwp:什麼情況下,什麼時候該對象已經刪除了複製構造函數?爲什麼需要該副本? –