std::call_once
ensures execution of a function exactly once by competing threads. It throws std::system_error
in case it cannot complete its task.
Used in conjunction with std::once_flag
.
#include <mutex>
#include <iostream>
std::once_flag flag;
void do_something(){
std::call_once(flag, [](){std::cout << "Happens once" << std::endl;});
std::cout << "Happens every time" << std::endl;
}