C++ 布尔复合赋值线程安全



如下所述,这些都不是线程安全的,因为它不是原子的anyTrue即使它在某些硬件上的行为可能是原子的:

#include <thread>
bool blah() {
    return true; // or false
}
int main()
{
    bool anyTrue = false;
    // I think foo should be thread safe:
    auto foo = [&] {
        if (blah()) {
            anyTrue = true;
        }
    };
    // but I'm not confident about bar:
    auto bar = [&] {
        anyTrue &= blah();
    };
    // parallel foo:
    std::thread t1 (foo);
    std::thread t2 (foo);
    t1.join();
    t2.join();
    // parallel bar:
    std::thread t3 (bar);
    std::thread t4 (bar);
    t3.join();
    t4.join();
}

如果我做anyTrue atomic它不再编译,因为它没有operator&= atomic

#include <atomic>
#include <thread>
bool blah() {
    return true; // or false
}
int main()
{
    std::atomic< bool > anyTrue (false);
    // I think foo should be thread safe:
    auto foo = [&] {
        if (blah()) {
            anyTrue = true;
        }
    };
    // but I'm not confident about bar:
    auto bar = [&] {
        anyTrue &= blah(); // error: no viable overloaded '&='
    };
    // parallel foo:
    std::thread t1 (foo);
    std::thread t2 (foo);
    t1.join();
    t2.join();
    // parallel bar:
    std::thread t3 (bar);
    std::thread t4 (bar);
    t3.join();
    t4.join();
}

这里根本没有线程安全:

  1. std::rand既不是可重入的,也不是线程安全的。事实上,它保持着一个内部种子,所以你在这里处于深水中。

  2. &=需要读取、修改,然后是写入。这不是线程安全的。

  3. 赋值=可能不是线程安全的(例如,两个线程可能同时写入同一内存,结果未定义。这是一个很好的老式数据竞赛。谁能说你的平台上没有一个真正的胖bool

最新更新