使用 std::call_once 时线程连接挂起
Thread Join hangs while using `std::call_once`
我想了解为什么 std::call_once
和 std::once_flag
我的节目
#include<iostream>
#include<thread>
#include<mutex>
#include<condition_variable>
using namespace std;
once_flag once;
void test(int i){
if(i==1){
cout<<"will be called again"<<endl;
throw exception();
}
cout<<"wont be called again"<<endl;
}
void caller(int i){
try{
call_once(once, test, i);
}catch(...){cout<<"caught"<<endl;}
}
int main(){
int val = 1;
thread t1(caller,1);
thread t2(caller,2);
thread t3(caller,2);
t1.join();t2.join();t3.join();
}
终端输出:1 will be called again\n caught\n wont be called again\n
这只是挂起,有时它会完成但大多数时候它会挂起,我认为它的竞争条件但无法弄清楚为什么它会发生。
我在这里找到了同样的例子 https://en.cppreference.com/w/cpp/thread/call_once
这似乎是 gcc (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66146) 中的一个错误。
您可以通过在您的示例中构造 t1
之后插入一个短暂的睡眠来重现该问题,例如
std::thread t1(caller,1);
using namespace std::chrono_literals;
std::this_thread::sleep_for(1ms);
我想了解为什么 std::call_once
和 std::once_flag
我的节目
#include<iostream>
#include<thread>
#include<mutex>
#include<condition_variable>
using namespace std;
once_flag once;
void test(int i){
if(i==1){
cout<<"will be called again"<<endl;
throw exception();
}
cout<<"wont be called again"<<endl;
}
void caller(int i){
try{
call_once(once, test, i);
}catch(...){cout<<"caught"<<endl;}
}
int main(){
int val = 1;
thread t1(caller,1);
thread t2(caller,2);
thread t3(caller,2);
t1.join();t2.join();t3.join();
}
终端输出:1 will be called again\n caught\n wont be called again\n
这只是挂起,有时它会完成但大多数时候它会挂起,我认为它的竞争条件但无法弄清楚为什么它会发生。
我在这里找到了同样的例子 https://en.cppreference.com/w/cpp/thread/call_once
这似乎是 gcc (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66146) 中的一个错误。
您可以通过在您的示例中构造 t1
之后插入一个短暂的睡眠来重现该问题,例如
std::thread t1(caller,1);
using namespace std::chrono_literals;
std::this_thread::sleep_for(1ms);