Код: Выделить всё
#include
#include
#include
int main(){
std::atomic val = 0;
std::atomic flag = false;
auto t1 = std::thread([](){
if(val.load(std::memory_order::relaxed)==1){ // #1
flag.store(true,std::memory_order::relaxed); // #2
}
});
auto t2 = std::thread([](){
while(!flag.load(std::memory_order::relaxed)); // #3
val.store(1,std::memory_order::relaxed); // #4
});
t1.join();
t2.join();
std::cout `#2` was executed
`#2` was executed -> `#1` was true
`#1` was true -> `#4` was executed
Код: Выделить всё
#4Подробнее здесь: https://stackoverflow.com/questions/798 ... g-the-oota
Мобильная версия