#include <atomic> #include <iostream> #include <thread> std::atomic<char const*> data; std::atomic_flag flag = ATOMIC_FLAG_INIT; void producer() { // A, Store data.store("success", std::memory_order_relaxed); // prevents reordering of Store-Store, Load-Store => A happens before B std::atomic_thread_fence(std::memory_order_release); // B, Store flag.test_and_set(std::memory_order_relaxed); } void consumer() { // C, Load, waits B while (!flag.test(std::memory_order_relaxed)); // prevents reordering of Store-Load, Load-Load => C happens before D std::atomic_thread_fence(std::memory_order_acquire); // D, Load std::cout << data.load(std::memory_order_relaxed) << '\n'; } /* * A happens before B * C waits B * C happens before D * therefore, A happends before D */ int main() { std::thread tp{producer}; std::thread tc{consumer}; tp.join(); tc.join(); }


주석 내용 맞지? 아토믹 머리 아프당ㅠㅠ