I'm having trouble understanding a part of the C++11 std::atomic_short behavior.
I set either 0 or 255 as value for an atomic_short variable.
But .load() says that the value is neither 0 or 255.
I want one thread to write the atomic variable and I want another thread read it.
Environment:
Intel Core i5
OSX 10.11.6
clang (Xcode7.3.1)
#include <iostream>
#include <atomic>
#include <thread>
std::atomic_short value = ATOMIC_VAR_INIT(0);
void process1() {
bool flag = false;
for (int i = 0; i < 100000; ++i){
std::this_thread::yield;
if (flag){
value.store(255);
} else {
value.store(0);
}
flag = !flag;
}
}
void process2() {
for (int i = 0; i < 100000; ++i){
std::this_thread::yield;
if (value.load() != 255 && value.load() != 0){
printf("warningA! %d\n", i);
}
}
}
int main(int argc, char** argv) {
value.store(0);
std::thread t1(process1);
std::thread t2(process2);
t1.join();
t2.join();
return 0;
}
warningA! 3
warningA! 1084
warningA! 1093
The problem is that you have 2 individual loads which makes your comparison non-atomic. Instead, load the value once and then compare:
void process2() {
for (int i = 0; i < 100000; ++i){
std::this_thread::yield;
auto currentValue = value.load();
if (currentValue != 255 && currentValue != 0){
printf("warningA! %d\n", i);
}
}
}
live example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With