C++11: What prevents stores from lifting past the start of a lock's critical section?
问题 My understanding is that a spinlock can be implemented using C++11 atomics with an acquire-CAS on lock and a release-store on unlock, something like this: class SpinLock { public: void Lock() { while (l_.test_and_set(std::memory_order_acquire)); } void Unlock() { l_.clear(std::memory_order_release); } private: std::atomic_flag l_ = ATOMIC_FLAG_INIT; }; Consider its use in a function that acquires a lock and then does a blind write to some shared location: int g_some_int_; void BlindWrite(int