How different std::memory_order types work? (seq_cst, acquire/release and others) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How different std::memory_order types work? (seq_cst, acquire/release and others)

Please, could someone explain or share a link to articles or videos about it? This is a really difficult topic in atomics and C++, and I don't get it at all. Maybe there are any good examples of the difference between memory orders?

25th Jun 2023, 7:24 PM
Patrick
Patrick - avatar
5 Answers
+ 2
In C++, the std::memory_order types are used to specify the ordering constraints for memory operations in a multi-threaded environment. There are several types of memory_order, each with its own set of rules and behaviors. 1. seq_cst (sequential consistency): This is the strongest memory ordering constraint. It guarantees that all memory operations are executed in a total order, as if they were executed by a single thread. This means that all threads will see the same sequence of events, regardless of their execution order. 2. acquire/release: These are weaker memory ordering constraints that allow for more relaxed synchronization between threads. An acquire operation ensures that all preceding memory operations are visible to other threads before the acquire operation is executed. A release operation ensures that all subsequent memory operations are visible to other threads after the release operation is executed.
25th Jun 2023, 9:27 PM
abdulrahman farhat
abdulrahman farhat - avatar
+ 2
3. relaxed: This is the weakest memory ordering constraint. It allows for maximum flexibility in terms of synchronization between threads, but also provides the least amount of guarantees about the order in which memory operations will be executed. 4. consume: This is a special type of acquire operation that is used specifically for reading data from shared memory structures. It ensures that any data read from shared memory is consistent with respect to other data read from the same structure. Overall, choosing the appropriate std::memory_order type depends on the specific requirements of your program and how you want to synchronize access to shared data between multiple threads.
25th Jun 2023, 9:28 PM
abdulrahman farhat
abdulrahman farhat - avatar
+ 1
abdulrahman farhat thank you! could you explain, what memory operations are involved? on atomic variables?
26th Jun 2023, 8:03 AM
Patrick
Patrick - avatar
+ 1
Atomic variables in C++ support the following memory operations: 1. Load: This operation reads the current value of the atomic variable. 2. Store: This operation writes a new value to the atomic variable. 3. Exchange: This operation atomically sets a new value to the atomic variable and returns its previous value. 4. Compare-and-swap (CAS): This operation compares the current value of the atomic variable with an expected value, and if they are equal, sets a new value to the atomic variable. Otherwise, it does nothing. 5. Fetch-and-add (FAA): This operation atomically adds a given value to the current value of the atomic variable and returns its previous value. 6. Fetch-and-sub (FAS): This operation atomically subtracts a given value from the current value of the atomic variable and returns its previous value. 7. Fetch-and-or (FAO): This operation atomically performs a bitwise OR between a given value and the current value of the atomic variable and returns its previous value.
26th Jun 2023, 8:05 AM
abdulrahman farhat
abdulrahman farhat - avatar
+ 1
8. Fetch-and-and (FAA): This operation atomically performs a bitwise AND between a given value and the current value of the atomic variable and returns its previous value. All these operations ensure that multiple threads can access and modify an atomic variable without causing race conditions or data inconsistencies
26th Jun 2023, 8:06 AM
abdulrahman farhat
abdulrahman farhat - avatar