Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我能';我不理解奇怪的std::atomic_short.load()行为_C++_C++11 - Fatal编程技术网

C++ 我能';我不理解奇怪的std::atomic_short.load()行为

C++ 我能';我不理解奇怪的std::atomic_short.load()行为,c++,c++11,C++,C++11,我很难理解C++11 std::atomic_short行为的一部分。 我将0或255设置为原子_短变量的值。 但是.load()表示该值既不是0也不是255。 我希望一个线程写入原子变量,另一个线程读取它 环境: 英特尔核心i5 OSX 10.11.6 叮当声(代码7.3.1) 问题是您有两个独立的加载s,这使得您的比较不是原子的。相反,加载一次该值,然后比较: void process2() { for (int i = 0; i < 100000; ++i){

我很难理解C++11 std::atomic_short行为的一部分。
我将0或255设置为原子_短变量的值。
但是.load()表示该值既不是0也不是255。
我希望一个线程写入原子变量,另一个线程读取它

环境:
英特尔核心i5
OSX 10.11.6
叮当声(代码7.3.1)


问题是您有两个独立的
加载
s,这使得您的比较不是原子的。相反,
加载一次该值,然后比较:

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);
        }
    }
}
void process2(){
对于(int i=0;i<100000;++i){
std::this_thread::yield;
自动currentValue=value.load();
如果(currentValue!=255&¤tValue!=0){
printf(“警告!%d\n”,i);
}
}
}

warningA! 3
warningA! 1084
warningA! 1093
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);
        }
    }
}