Ich habe Probleme, einen Teil der C++ 11 std :: atomic_short Verhalten zu verstehen.
Ich habe entweder 0 oder 255 als Wert für eine atomic_short-Variable festgelegt.
Aber .load() sagt, dass der Wert weder 0 noch 255 ist.
Ich möchte einen Thread die atomare Variable schreiben und ich möchte einen anderen Thread lesen.Ich kann nicht komisch std :: atomic_short.load() Verhalten
Umwelt:
Intel Core i5
OSX 10.11.6
Klirren (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