Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there bare c++ 11 (or boost) replacement for InterlockedExchangePointer?

Looking for portable, simple and elegant replacement for Win32 InterlockedExchangePointer. Ideally using C++11 only but boost will do too.

like image 223
kreuzerkrieg Avatar asked Mar 24 '15 08:03

kreuzerkrieg


1 Answers

The standard atomic types have an atomic exchange function. So the Microsoftese

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

would become

std::atomic<T*> target;
old_value = target.exchange(new_value);
like image 89
Mike Seymour Avatar answered Sep 21 '22 17:09

Mike Seymour