0
我正在學習使用線程。而且我發現我可以使用下面的a thread related problm
mutex mx;
void func(int id)
{
mx.lock();
cout << "hey , thread:"<<id << "!" << endl;
mx.unlock();
}
int main(){
vector<thread> threads;
for(int i = 0 ; i < 5 ; i++)
threads.emplace_back(thread(func , i));
for(thread & t : threads)
t.join();
return 0;
}
,而我不能做的main()
for(int i = 0 ; i < 5 ; i ++)
{
thread t(func , i);
threads.emplace_back(t);
}
任何人能解釋這一點?
非常感謝
你收到了哪些錯誤信息? –
當使用'emplace_back'時,元素被構造就位。所有你想傳遞的是構造函數的參數,而不是創建一個不必要的對象。 – chris