문제

Global:

std::atomic<T*> a, b;

In thread A:

// this like std::swap(a, b), but is it atomic?
a.exchange(b.exchange(a));

In thread B:

T* p = a.load();
doSomethine(p);
도움이 되었습니까?

해결책

Sequences of atomic operations aren't atomic. Something could happen between b.exchange and a.exchange, and observe that both a and b have the same value (the original value of a).

If you don't want this, use some type of lock.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top