C++11 アトミック操作で unique_ptr を安全に移動することは可能ですか?
Currently I have a code like this
std::unique_ptr<SyncToken> DataManager::borrowSyncToken()
{
std::unique_lock<std::mutex> syncTokenLock(syncTokenMutex);
return std::move(syncToken);
}
I am wondering whether there is some more elegant way, like simply declaring:
std::atomic<std::unique_ptr<SyncToken>> syncToken;
and avoiding the need of mutex. Or possibly I don't need to care about the lock here at all and std::move is already atomic?
After research I made so far it seems to me:
- the std::move itself is not atomic and needs to have some synchronization around otherwise 2 threads calling my method concurrently may end up with 2 copies of some undefined pointers.
- the std::atomic declaration compiles for me, but I don't know how to initialize it and and make the move.