I want to find out if the std::call_once lock free. There are call_once implementations using mutex. But why should we use mutex? I tried to write simple implementation using atomic_bool and CAS operation. Is the code thread safe?
#include <iostream>
#include <thread>
#include <atomic>
#include <unistd.h>
using namespace std;
using my_once_flag = atomic<bool>;
void my_call_once(my_once_flag& flag, std::function<void()> foo) {
bool expected = false;
bool res = flag.compare_exchange_strong(expected, true,
std::memory_order_release, std::memory_order_relaxed);
if(res)
foo();
}
my_once_flag flag;
void printOnce() {
usleep(100);
my_call_once(flag, [](){
cout << "test" << endl;
});
}
int main() {
for(int i = 0; i< 500; ++i){
thread([](){
printOnce();
}).detach();
}
return 0;
}
Your proposed implementation is not thread safe. It does guarantee that foo()
will only be called once through this code, but it does not guarantee that all threads will see side effects from the call to foo()
. Suppose that thread 1 executes the compare and gets true, then the scheduler switches to thread 2, before thread 2 calls foo()
. Thread 2 will get false, skip the call to foo()
, and move on. Since the call to foo()
has not been executed, thread 2 can continue executing before any side effects from foo()
have occurred.