Thread-safe lock-free array

后端 未结 3 1902
伪装坚强ぢ
伪装坚强ぢ 2020-12-19 04:01

I have a C++ library, which supposed to do some computations on multiple threads. I made independent threads code (i.e. there are no shared variables between them), except f

3条回答
  •  死守一世寂寞
    2020-12-19 04:10

    This line

    AtomicUInt64 ( std::atomic a ) : atomic ( atomic.load() ) {}
    

    You're completely ignoring the argument you pass in, You probably want it to be a.load() and you probably want to take elements by const reference so they aren't copied.

    AtomicUInt64 (const std::atomic& a) : atomic (a.load()) {}
    

    As for what you're doing, I'm not sure if it is correct. The modification of the variables inside the array will be atomic, but if the vector is modified or reallocated (which is possible with push_back), then there's nothing to guarantee your array modifications will work between threads and be atomic.

提交回复
热议问题