InterlockedExchangePointer是否有替换c ++ 11(或boost)?

时间:2015-03-24 08:49:26

标签: c++ multithreading winapi c++11 boost

寻找Win32 InterlockedExchangePointer的便携,简单和优雅的替代品。理想情况下,只使用C ++ 11,但也可以使用。

1 个答案:

答案 0 :(得分:10)

standard atomic types具有原子exchange功能。所以Microsoftese

PVOID volatile target;
old_value = InterlockedExchangePointer(&target, new_value);

会变成

std::atomic<T*> target;
old_value = target.exchange(new_value);