[英]Boost thread_group blocks when calling add_thread from thread, why?
我編寫了一個類似於以下結構的多線程程序(我省略了互斥鎖和無關的代碼),當從線程調用boost::thread_group.add_thread()
時,它阻塞了該調用。 有沒有解決的辦法,所以通話不會阻塞?
boost::thread_group group;
void threaded_function2()
{
}
void threaded_function()
{
if( condition)
{
boost::thread *t3 = new boost::thread( boost::bind( &threaded_function2));
group.add_thread( t3); // <-- Blocks on this call
}
}
int main()
{
boost::thread *t1 = new boost::thread( boost::bind( &threaded_function));
boost::thread *t2 = new boost::thread( boost::bind( &threaded_function));
group.add_thread( t1);
group.add_thread( t2);
group.join_all();
return 0;
}
感謝大家。
如果我錯了,請糾正我,但是這里可能發生的情況是join_all調用在添加線程之前運行,使thread_group對象阻塞,直到釋放其他線程為止。 一種解決方案是在主函數上創建一個互斥鎖,以等待threaded_function完成以調用join_all方法。 但是,這是錯誤的設計。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.