I'm looking for a example on synchronization regarding a release fence and a atomic acquire operation. All release fence examples i have found are fence-fence synchronization.
Asked
Active
Viewed 68 times
1
-
[The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242) mentions the fantastic **C++ Concurrency In Action**, which would explain important fence details that may not be obvious in a simple code example. – Drew Dormann Jan 07 '21 at 15:38
-
and yet, a example is needed to be sure of the understanding. and that book only had release fance - acquire fence. i found no release fence - acquire operation example. and since my processor won't generate any asserts i have no way of knowing if i learned things right or not unless i'm given a proper example – TheNegative Jan 07 '21 at 15:51
-
If I understand this question, you have seen synchronization examples using acquire/release semantics, but you are specifically looking for an example that uses a `std::atomic_thread_fence(std::memory_order_release)` with the store and a `std::atomic
::load(std::memory_order_acquire)` for the load? – Drew Dormann Jan 07 '21 at 18:54 -
yes. that would suffice. – TheNegative Jan 07 '21 at 19:07
1 Answers
2
It is straightforward.. You can use a release fence and an acquire operation together like this:
int main()
{
int x = 0;
std::atomic<bool> flag{false};
std::thread t1{[&]
{
x = 42;
std::atomic_thread_fence(std::memory_order_release);
flag.store(true, std::memory_order_relaxed);
}};
std::thread t2{[&]
{
if (flag.load(std::memory_order_acquire))
{
assert(x == 42); // cannot fire
}
}};
t1.join(); t2.join();
}
Note that the relaxed store must be sequenced after the release fence.

LWimsey
- 6,189
- 2
- 25
- 53
-
1thank you. this clears it up because someone on cppreference got confused about it and even made this revision : https://en.cppreference.com/mwiki/index.php?title=cpp/atomic/atomic_thread_fence&oldid=119464 so i had to be sure. – TheNegative Jan 08 '21 at 13:50