21 Aralık 2020 Pazartesi

std::memory_order Acquire ve Release

Giriş 
std::memory_order::acquire ve std::memory_order::release beraber kullanılır.

Örnek
Elimizde şöyle bir kod olsun
atomic<int> Guard(0);
int Payload = 0;
Okuma yapan tarafta şöyle yaparız
g = Guard.load(memory_order_acquire);
if (g != 0) p = Payload;
Yazma yapan tarafta şöyle yaparız
Payload = 42;
Guard.store(1, memory_order_release);
Açıklaması şöyle
Guard is a C++11 atomic integer, while Payload is just a plain int. Both variables are initially zero.
Basically, the purpose of Guard is to protect access to Payload using acquire semantics.
The main thread won’t attempt to read from Payload until Guard is non-zero.

At some point, an asynchronous task (running in another thread) comes along, assigns 42 to Payload, then sets Guard to 1 with release semantics.

Here, we’ve used acquire and release semantics to pass a simple non-atomic integer Payload between threads,

Hiç yorum yok:

Yorum Gönder