std::memory_order
Aus cppreference.com
![]() |
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
definiert in Header <atomic>
|
||
enum memory_order { memory_order_relaxed, |
(seit C++11) | |
std::memory_order
gibt an, wie nicht-atomare Speicherzugriffe um einer atomaren Operation bestellt werden. Der Grund dafür ist, dass, wenn mehrere Threads gleichzeitig lesen und schreiben mehrere Variablen auf Multi-Core-Systeme, ein Thread könnte' s. Die Werte ändern sich in anderer Reihenfolge als ein anderer Thread hat sie geschrieben. Auch kann die Reihenfolge der Veränderungen offensichtlich unterschiedlich auf mehrere Leserthreads. Sicherstellen, dass der gesamte Speicher zur atomaren Variablen zugreift werden sequenzielle kann weh Leistung in einigen Fällen. std::memory_order
ermöglicht, die genauen Bedingungen, dass der Compiler muss durchzusetzen geben .Original:
std::memory_order
specifies how non-atomic memory accesses are to be ordered around an atomic operation. The rationale of this is that when several threads simultaneously read and write to several variables on multi-core systems, one thread might see the values change in different order than another thread has written them. Also, the apparent order of changes may be different across several reader threads. Ensuring that all memory accesses to atomic variables are sequential may hurt performance in some cases. std::memory_order
allows to specify the exact constraints that the compiler must enforce.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Es ist möglich, benutzerdefinierte Speicher, um für jede atomare Operation in der Bibliothek angeben, über einen zusätzlichen Parameter. Der Standardwert ist std::memory_order_seq_cst .
Original:
It's possible to specify custom memory order for each atomic operation in the library via an additional parameter. The default is std::memory_order_seq_cst.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Inhaltsverzeichnis |
[Bearbeiten] Konstanten
definiert in Header
<atomic> | |
Value
Original: Value The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
Explanation |
memory_order_relaxed
|
'Relaxed' bestellen: Es gibt keine Einschränkungen für Neuordnung der Speicherzugriffe um die atomare Variable .
Original: Relaxed ordering: there are no constraints on reordering of memory accesses around the atomic variable. The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
memory_order_consume
|
'Verbrauchen' Betrieb: keine liest den aktuellen Thread abhängig vom Wert derzeit geladen werden, bevor dieser Belastung können nachbestellt werden. Dies gewährleistet, dass schreibt abhängigen Variablen in anderen Threads, die die gleiche atomare Variable freisetzen, sind in den aktuellen Thread sichtbar. Auf den meisten Plattformen, wirkt sich diese Compiler-Optimierung nur .
Original: Consume operation: no reads in the current thread dependent on the value currently loaded can be reordered before this load. This ensures that writes to dependent variables in other threads that release the same atomic variable are visible in the current thread. On most platforms, this affects compiler optimization only. The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
memory_order_acquire
|
'Acquire' Betrieb: keine liest den aktuellen Thread kann vor dieser Belastung erst nachbestellt werden. Dadurch wird sichergestellt, dass alle in anderen Threads, die die gleiche atomare Variable freizugeben sichtbar sind in der aktuellen Thread schreibt .
Original: Acquire operation: no reads in the current thread can be reordered before this load. This ensures that all writes in other threads that release the same atomic variable are visible in the current thread. The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
memory_order_release
|
'Release' Betrieb: keine schreibt in der aktuellen Thread kann nach diesen Shop nachbestellt werden. Dadurch wird sichergestellt, dass alle Schreibvorgänge in den aktuellen Thread sichtbar in anderen Threads, die die gleiche atomare Variable zu erwerben .
Original: Release operation: no writes in the current thread can be reordered after this store. This ensures that all writes in the current thread are visible in other threads that acquire the same atomic variable. The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
memory_order_acq_rel
|
'Acquire-Release' Betrieb: keine liest den aktuellen Thread kann vor dieser Belastung nachbestellt so gut wie keine, schreibt in der aktuellen Thread kann nach diesen Shop nachbestellt werden. Die Bedienung ist Lese-Modifizier-Schreib-Vorgang. Es wird sichergestellt, dass alle Schreibvorgänge in anderen Threads, die dieselbe atomare Variable freizugeben sichtbar vor der Änderung sind und die Änderung ist sichtbar in anderen Threads, die die gleiche atomare Variable zu erwerben .
Original: Acquire-release operation: no reads in the current thread can be reordered before this load as well as no writes in the current thread can be reordered after this store. The operation is read-modify-write operation. It is ensured that all writes in another threads that release the same atomic variable are visible before the modification and the modification is visible in other threads that acquire the same atomic variable. The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
memory_order_seq_cst
|
'Sequential Ordnung'. Der Betrieb verfügt über die gleiche Semantik wie Erwerb-release Betrieb und zusätzlich sequenziell konsistenten Betrieb der Bestellung .
Original: Sequential ordering. The operation has the same semantics as acquire-release operation, and additionally has sequentially-consistent operation ordering. The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
[Bearbeiten] Relaxed Ordnung
Atomare Operationen getaggt std::memory_order_relaxed folgende Eigenschaften aufweisen:
Original:
Atomic operations tagged std::memory_order_relaxed exhibit the following properties:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- Keine Bestellung von anderen Speicherzugriffe ist immer gewährleistet. Dies bedeutet, dass es nicht möglich ist, mehrere Threads mit dem atomaren variablen synchronisieren .Original:No ordering of other memory accesses is ensured whatsoever. This means that it is not possible to synchronize several threads using the atomic variable.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - Liest und schreibt auf die atomare Variable selbst bestellt werden. Sobald ein Thread liest einen Wert, eine spätere Lese von dem selben Thread aus dem gleichen Objekt kann nicht ergeben einen früheren Wert .Original:Reads and writes to the atomic variable itself are ordered. Once a thread reads a value, a subsequent read by the same thread from the same object can not yield an earlier value.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Beispielsweise mit
x
und y
zunächst NullOriginal:
For example, with
x
and y
initially zero,The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
// Thread 1:
r1 = y.load(memory_order_relaxed);
x.store(r1, memory_order_relaxed);
// Thread 2:
r2 = x.load(memory_order_relaxed);
y.store(42, memory_order_relaxed);
darf
r1 == r2 == 42
produzieren .Original:
is allowed to produce
r1 == r2 == 42
.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[Bearbeiten] Release-Verbrauchen Bestellung
Wenn eine atomare Speicher markiert ist und eine atomare std::memory_order_release Last von der gleichen Variable std::memory_order_consume tagged zeigen die Operationen die folgenden Eigenschaften:
Original:
If an atomic store is tagged std::memory_order_release and an atomic load from the same variable is tagged std::memory_order_consume, the operations exhibit the following properties:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- Kein schreibt in der Writer-Thread kann nach dem atomaren Shop nachbestellt werdenOriginal:No writes in the writer thread can be reordered after the atomic storeThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - Kein liest oder schreibt abhängig von dem Wert von atomaren Last empfangen, bevor die atomare Last nachbestellbar. "Abhängig von" bedeutet, dass die Adresse oder Wert aus dem Wert des atomaren Variable berechnet. Diese Form der Synchronisation zwischen Threads als "Abhängigkeit ordering" bekannt .Original:No reads or writes dependent on the value received from atomic load can be reordered before the atomic load. "Dependent on" means that the address or value is computed from the value of the atomic variable. This form of synchronization between threads is known as "dependency ordering".The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - Die Synchronisation wird nur zwischen den Themen etabliert Loslassen und Konsum die gleiche atomare Variable. Andere Gewinde sehen unterschiedliche Reihenfolge der Speicherzugriffe als eine oder beide der synchronisierten Threads .Original:The synchronization is established only between the threads releasing and consuming the same atomic variable. Other threads can see different order of memory accesses than either or both of the synchronized threads.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - Die Synchronisation ist transitiv. Das heißt, wenn wir die folgende Situation:Original:The synchronization is transitive. That is, if we have the following situation:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- Thema A veröffentlicht atomare Variable a .Original:Thread A releases atomic variable a.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - Thema B verbraucht atomare Variable a .Original:Thread B consumes atomic variable a.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - Atomic variable b ist abhängig von a .Original:Atomic variable b is dependent on a.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - Thema B Versionen atomare Variable b .Original:Thread B releases atomic variable b.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - Thema C verbraucht oder erwirbt atomare Variable b .Original:Thread C consumes or acquires atomic variable b.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
-
- Dann wird nicht nur A und B oder B und C sind synchronisiert, aber A und C auch. Das heißt, alle Schreibvorgänge von dem Thread A, die vor der Veröffentlichung von eingeführt wurden a garantiert abgeschlossen werden, sobald Thema C stellt den Speicher, um b .Original:Then not only A and B or B and C are synchronized, but A and C also. That is, all writes by the thread A that were launched before the release of a are guaranteed to be completed once thread C observes the store to b.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Auf allen Mainstream-CPUs, andere als DEC Alpha, Abhängigkeit Bestellung automatisch erfolgt, sind keine zusätzlichen CPU-Anweisungen für diese Synchronisation ausgestellt wird, nur bestimmte Compiler-Optimierungen betroffen sind (zB der Compiler von der Durchführung spekulativen Lasten auf die Objekte, die in den beteiligten verboten sind Abhängigkeit Kette)
Original:
On all mainstream CPUs, other than DEC Alpha, dependency ordering is automatic, no additional CPU instructions are issued for this synchronization mode, only certain compiler optimizations are affected (e.g. the compiler is prohibited from performing speculative loads on the objects that are involved in the dependency chain)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[Bearbeiten] Lassen Sequenz
Wenn einige atomar ist store-released und einige andere Themen Lese-Modifizieren-Schreib-Operationen auf diesem atomaren, wird ein "Release-Sequenz" gebildet: Alle Themen, die die Lese-Modifikations-Schreibvorgänge auf dem gleichen Atom Synchronisierung mit dem ersten Faden und führen einander, auch wenn sie keine
memory_order_release
Semantik. Damit können einzelne Produzent - mehrere Verbraucher Situationen möglich, ohne unnötige Synchronisation zwischen einzelnen Konsumenten-Threads .Original:
If some atomic is store-released and several other threads perform read-modify-write operations on that atomic, a "release sequence" is formed: all threads that perform the read-modify-writes to the same atomic synchronize with the first thread and each other even if they have no
memory_order_release
semantics. This makes single producer - multiple consumers situations possible without imposing unnecessary synchronization between individual consumer threads.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[Bearbeiten] Release-Erwerben Bestellung
Wenn eine atomare Speicher markiert ist und eine atomare std::memory_order_release Last von der gleichen Variable std::memory_order_acquire tagged zeigen die Operationen die folgenden Eigenschaften:
Original:
If an atomic store is tagged std::memory_order_release and an atomic load from the same variable is tagged std::memory_order_acquire, the operations exhibit the following properties:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- Kein schreibt in der Writer-Thread kann nach dem atomaren Shop nachbestellt werdenOriginal:No writes in the writer thread can be reordered after the atomic storeThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - Kein liest der Leser Thread kann vor der atomaren Belastung nachbestellt werden .Original:No reads in the reader thread can be reordered before the atomic load.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - Die Synchronisation wird nur zwischen den Fäden hergestellt' und freisetzenden Erfassen dieselbe Ordnungszahl variabel. Andere Gewinde sehen unterschiedliche Reihenfolge der Speicherzugriffe als eine oder beide der synchronisierten Threads .Original:The synchronization is established only between the threads releasing and acquiring the same atomic variable. Other threads can see different order of memory accesses than either or both of the synchronized threads.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - Die Synchronisation ist transitiv. Das heißt, wenn wir die folgende Situation:Original:The synchronization is transitive. That is, if we have the following situation:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- Thema A veröffentlicht atomare Variable a .Original:Thread A releases atomic variable a.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - Thema B verbraucht atomare Variable a .Original:Thread B consumes atomic variable a.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - Thema B Versionen atomare Variable b .Original:Thread B releases atomic variable b.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - Thema C verbraucht oder erwirbt atomare Variable b .Original:Thread C consumes or acquires atomic variable b.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
-
- Dann wird nicht nur A und B oder B und C sind synchronisiert, aber A und C auch. Das heißt, alle Schreibvorgänge von dem Thread A, die vor der Veröffentlichung von eingeführt wurden a garantiert abgeschlossen werden, sobald Thema C stellt den Speicher, um b .Original:Then not only A and B or B and C are synchronized, but A and C also. That is, all writes by the thread A that were launched before the release of a are guaranteed to be completed once thread C observes the store to b.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Auf stark geordneten Systemen (x86, SPARC, IBM Mainframe), Release-Erwerb der Bestellung erfolgt automatisch. Keine zusätzlichen CPU-Anweisungen sind für diese Synchronisation ausgestellt wird, nur bestimmte Compiler-Optimierungen betroffen sind (zB der Compiler von bewegten nicht-atomare Läden vorbei atomaren store-relase verboten oder führen nicht-atomare Belastungen früher als die atomare Last zu erwerben)
Original:
On strongly-ordered systems (x86, SPARC, IBM mainframe), release-acquire ordering is automatic. No additional CPU instructions are issued for this synchronization mode, only certain compiler optimizations are affected (e.g. the compiler is prohibited from moving non-atomic stores past the atomic store-relase or perform non-atomic loads earlier than the atomic load-acquire)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[Bearbeiten] Sequenziell konsistenten Bestellung
Wenn ein Speicher und ein Atom markiert ist und eine atomare std::memory_order_seq_cst Last von der gleichen Variable std::memory_order_seq_cst, markiert dann die Operationen die folgenden Eigenschaften aufweisen:
Original:
If an atomic store and an is tagged std::memory_order_seq_cst and an atomic load from the same variable is tagged std::memory_order_seq_cst, then the operations exhibit the following properties:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- Kein schreibt in der Writer-Thread kann nach dem atomaren Shop nachbestellt werdenOriginal:No writes in the writer thread can be reordered after the atomic storeThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - Kein liest der Leser Thread kann vor der atomaren Belastung nachbestellt werden .Original:No reads in the reader thread can be reordered before the atomic load.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - Die Synchronisation erfolgt zwischen allen atomare Operationen getaggt std::memory_order_seq_cst etabliert. Alle Threads mit solchen atomaren Operation sehen die gleiche Reihenfolge der Speicherzugriffe .Original:The synchronization is established between all atomic operations tagged std::memory_order_seq_cst. All threads using such atomic operation see the same order of memory accesses.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Sequential-Bestellung ist notwendig für viele multiple Produzenten-multiple Verbraucher Situationen, in denen alle Verbraucher die Aktionen aller Hersteller vorkommende in der gleichen Reihenfolge beachten müssen .
Original:
Sequential ordering is necessary for many multiple producer-multiple consumer situations where all consumers must observe the actions of all producers occurring in the same order.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Insgesamt sequenzielle Anordnung erfordert eine volle Speicher Zaun CPU-Befehlssätze auf allen Multi-Core-Systemen. Dies kann ein Performance-Engpass geworden, da zwingt sie alle Speicherzugriffe auf jeden Thread zu verbreiten .
Original:
Total sequential ordering requires a full memory fence CPU instruction on all multi-core systems. This may become a performance bottleneck since it forces all memory accesses to propagate to every thread.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[Bearbeiten] Beziehung mit volatil
{{{1}}}
Original:
{{{2}}}
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[Bearbeiten] Beispiele
[Bearbeiten] std::memory_order_relaxed
Das folgende Beispiel zeigt eine Aufgabe (Aktualisierung eines globalen Zähler), die Unteilbarkeit erfordert, aber keine Bestellung Einschränkungen, da nicht-atomare Speicher wird nicht eingebunden .
Original:
The following example demonstrates a task (updating a global counter) that requires atomicity, but no ordering constraints since non-atomic memory is not involved.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
#include <vector> #include <iostream> #include <thread> #include <atomic> std::atomic<int> cnt = ATOMIC_VAR_INIT(0); void f() { for(int n = 0; n < 1000; ++n) { cnt.fetch_add(1, std::memory_order_relaxed); } } int main() { std::vector<std::thread> v; for(int n = 0; n < 10; ++n) { v.emplace_back(f); } for(auto& t : v) { t.join(); } std::cout << "Final counter value is " << cnt << '\n'; }
Output:
Final counter value is 10000
[Bearbeiten] std::memory_order_release
and std::memory_order_consume
Dieses Beispiel zeigt die Abhängigkeit bestellt Synchronisation: die Integer-Daten nicht mit dem Zeiger auf eine Zeichenkette anhand eines Daten-Abhängigkeits-Beziehung stehen, damit sein Wert ist in der Consumer undefined .
Original:
This example demonstrates dependency-ordered synchronization: the integer data is not related to the pointer to string by a data-dependency relationship, thus its value is undefined in the consumer.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
#include <thread> #include <atomic> #include <cassert> #include <string> std::atomic<std::string*> ptr; int data; void producer() { std::string* p = new std::string("Hello"); data = 42; ptr.store(p, std::memory_order_release); } void consumer() { std::string* p2; while (!(p2 = ptr.load(std::memory_order_consume))) ; assert(*p2 == "Hello"); // never fires assert(data == 42); // may or may not fire } int main() { std::thread t1(producer); std::thread t2(consumer); t1.join(); t2.join(); }
[Bearbeiten] std::memory_order_release
and memory_order_acquire
Mutexe, gleichzeitig Warteschlangen und andere Erzeuger-Verbraucher-Situationen erfordern Release Bestellung im Verlag Thema und erwerben Bestellung im Consumer-Thread. Dieses Muster stellt paarweisen Synchronisierung zwischen Threads .
Original:
Mutexes, concurrent queues, and other producer-consumer situations require release ordering in the publisher thread and acquire ordering in the consumer thread. This pattern establishes pairwise synchronization between threads.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
#include <thread> #include <atomic> #include <cassert> #include <string> std::atomic<std::string*> ptr; int data; void producer() { std::string* p = new std::string("Hello"); data = 42; ptr.store(p, std::memory_order_release); } void consumer() { std::string* p2; while (!(p2 = ptr.load(std::memory_order_acquire))) ; assert(*p2 == "Hello"); // never fires assert(data == 42); // never fires } int main() { std::thread t1(producer); std::thread t2(consumer); t1.join(); t2.join(); }
[Bearbeiten] std::memory_order_acq_rel
Die folgende Beispiel zeigt, transitive Release-Erwerb Bestellung über drei Themen
Original:
The follow example demonstrates transitive release-acquire ordering across three threads
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
#include <thread> #include <atomic> #include <cassert> #include <vector> std::vector<int> data; std::atomic<int> flag = ATOMIC_VAR_INIT(0); void thread_1() { data.push_back(42); flag.store(1, std::memory_order_release); } void thread_2() { int expected=1; while (!flag.compare_exchange_strong(expected, 2, std::memory_order_acq_rel)) { expected = 1; } } void thread_3() { while (flag.load(std::memory_order_acquire) < 2) ; assert(data.at(0) == 42); // will never fire } int main() { std::thread a(thread_1); std::thread b(thread_2); std::thread c(thread_3); a.join(); b.join(); c.join(); }
[Bearbeiten] std::memory_order_seq_cst
Dieses Beispiel zeigt eine Situation, in der sequentiellen Reihenfolge ist notwendig. Jede andere Reihenfolge auslösen kann das behaupten, denn es wäre für die Fäden
c
und d
Änderungen an den atomics beobachten x
und y
in umgekehrter Reihenfolge möglich .
Original:
This example demonstrates a situation where sequential ordering is necessary. Any other ordering may trigger the assert because it would be possible for the threads
c
and d
to observe changes to the atomics x
and y
in opposite order.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
#include <thread> #include <atomic> #include <cassert> std::atomic<bool> x = ATOMIC_VAR_INIT(false); std::atomic<bool> y = ATOMIC_VAR_INIT(false); std::atomic<int> z = ATOMIC_VAR_INIT(0); void write_x() { x.store(true, std::memory_order_seq_cst); } void write_y() { y.store(true, std::memory_order_seq_cst); } void read_x_then_y() { while (!x.load(std::memory_order_seq_cst)) ; if (y.load(std::memory_order_seq_cst)) { ++z; } } void read_y_then_x() { while (!y.load(std::memory_order_seq_cst)) ; if (x.load(std::memory_order_seq_cst)) { ++z; } } int main() { std::thread a(write_x); std::thread b(write_y); std::thread c(read_x_then_y); std::thread d(read_y_then_x); a.join(); b.join(); c.join(); d.join(); assert(z.load() != 0); // will never happen }