Lock-free swap of two unique_ptr<T>
The idiomatic way to modify two variables atomically is to use a lock.
You can't do it for std::unique_ptr
without a lock. Even std::atomic<int>
doesn't provide a way to swap two values atomically. You can update one atomically and get its previous value back, but a swap is conceptually three steps, in terms of the std::atomic
API they are:
auto tmp = a.load();
tmp = b.exchange(tmp);
a.store(tmp);
This is an atomic read followed by an atomic read-modify-write followed by an atomic write. Each step can be done atomically, but you can't do all three atomically without a lock.
For a non-copyable value such as std::unique_ptr<T>
you can't even use the load
and store
operations above, but must do:
auto tmp = a.exchange(nullptr);
tmp = b.exchange(tmp);
a.exchange(tmp);
This is three read-modify-write operations. (You can't really use std::atomic<std::unique_ptr<T>>
to do that, because it requires a trivially-copyable argument type, and std::unique_ptr<T>
isn't any kind of copyable.)
To do it with fewer operations would need a different API that isn't supported by std::atomic
because it can't be implemented because as Stas's answer says, it isn't possible with most processors. The C++ standard is not in the habit of standardising functionality that is impossible on all contemporary architectures. (Not intentionally anyway!)
Edit: Your updated question asks about a very different problem, in the second example you don't need an atomic swap that affects two objects. Only global
is shared between threads, so you don't care if updates to local
are atomic, you just need to atomically update global
and retrieve the old value. The canonical C++11 way to do that is with std:atomic<T*>
and you don't even need a second variable:
atomic<T*> global;
void f() {
delete global.exchange(new T(...));
}
This is a single read-modify-write operation.
Lock-free swapping of two pointers
It seems there is no general lock-free solution for this problem. To do this, you need a possibility to atomically write new values into two non-continous memory locations. This is called DCAS
, but it is not available in Intel processors.
Lock-free transfer of ownership
This one is possible, as it is only needed to atomically save new value into global
and receive its old value. My first idea was to use CAS
operation. Take a look at the following code to get an idea:
std::atomic<T*> global;
void f() {
T* local = new T;
T* temp = nullptr;
do {
temp = global; // 1
} while(!std::atomic_compare_exchange_weak(&global, &temp, local)); // 2
delete temp;
}
Steps
- Remember current
global
pointer intemp
- Save
local
toglobal
ifglobal
is still equal totemp
(it wasn't changed by other thread). Try again if this is not true.
Actually, CAS
is overkill there, as we do not do anything special with old global
value before it is changed. So, we just can use atomic exchange operation:
std::atomic<T*> global;
void f() {
T* local = new T;
T* temp = std::atomic_exchange(&global, local);
delete temp;
}
See Jonathan's answer for even more short and elegant solution.
Anyway, you will have to write your own smart pointer. You can't use this trick with standard unique_ptr
.