cppreference.com documents this function as “fence between a thread and a signal handler executed in the same thread”. But I found no example on the Internet.
I wonder whether or not the following psuedo-code correctly illustrates the function of std::atomic_signal_fence():
int n = 0;
SignalObject s;
void thread_1()
{
s.wait();
std::atomic_signal_fence(std::memory_order_acquire);
assert(1 == n); // never fires ???
}
void thread_2()
{
n = 1;
s.signal();
}
int main()
{
std::thread t1(thread_1);
std::thread t2(thread_2);
t1.join(); t2.join();
}
No, your code does not demonstrate correct usage of
atomic_signal_fence. As you quote cppreference.com,atomic_signal_fenceonly perform synchronization between a signal handler and other code running on the same thread. That means that it does not perform synchronization between two different threads. Your example code shows two different threads.The C++ spec contains the following notes about this function:
Here’s an example of correct, if not motivating, usage:
The assertion, if encountered, is guaranteed to hold true.