From 2306e3e15fd880f26ba856bd257a3e1ef266777b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Daniel=20Kr=C3=BCgler?=
+Such two questions are sourced from StackOverflow:
+ Can the read operations in compare_exchange_strong in different two thread read the same value? For purposes of ordering, is atomic read-modify-write one operation or two?
+Given this example:
+
+In the current draft, the relevant phrasing that can interpret that only one read-modify-write operation reads the initial
+value false is
+Atomic read-modify-write operations shall always read the last value (in the modification order) written before the write
+associated with the read-modify-write operation.
+
+However, the wording can have two meanings, each kind of read can result in different explanations for the example
+ The check of the violation is done before the side effect of the RMW is in the modification order, i.e. the rule is
+just checked at the read point. The check of the violation is done after the side effect of the RMW is in the modification order, i.e. the rule is
+checked when RMW tries to add the side effect that is based on the read-value to the modification order, and that
+side effect wouldn't be added to the modification order if the rule was violated.
+With the first interpretation, the two RMW operations can read the same initial value because that value is indeed the last value
+in the modification order before such two RMW operations produce the side effect to the modification order.
+
+
+
+
+
+#include <iostream>
+#include <atomic>
+#include <thread>
+
+struct SpinLock{
+ std::atomic<bool> atomic_;
+ void lock(){
+ bool expected = false;
+ while (!atomic_.compare_exchange_strong(expected,true,std::memory_order_release,std::memory_order_relaxed)) {
+ }
+ }
+ void unlock(){
+ atomic_.store(false, std::memory_order_release);
+ }
+};
+
+int main(){
+ SpinLock spin{false};
+ auto t1 = std::thread([&](){
+ spin.lock();
+ spin.unlock();
+ });
+ auto t2 = std::thread([&](){
+ spin.lock();
+ spin.unlock();
+ });
+ t1.join();
+ t2.join();
+}
+
+
+
+
+
++Atomic read-modify-write operations shall always read the value from a side effect X, where X +immediately precedes the side effect of the read-modify-write operation in the modification order. +
+This wording keeps a similar utterance to
+This wording is relative to
Modify the
++ ++-10- Atomic read-modify-write operations shall always read the
+-11- Implementations should make atomic stores visible to atomic loads within a reasonable amount of time. + +lastvalue from a side effect X, +where X immediately precedes the side effect of the read-modify-write operation(in the +modification order) written before the write associated with the read-modify-write operation. +