交换两个unique_ptrs并不保证是线程安全的.
std::unique_ptr<T> a, b;
std::swap(a, b); // not threadsafe
Run Code Online (Sandbox Code Playgroud)
由于我需要原子指针交换,因为我喜欢所有权处理unique_ptr,是否有一种简单的方法将它们组合起来?
编辑:如果这是不可能的,我愿意接受替代方案.我至少想做这样的事情:
threadshared_unique_ptr<T> global;
void f() {
threadlocal_unique_ptr<T> local(new T(...));
local.swap_content(global); // atomically for global
}
Run Code Online (Sandbox Code Playgroud)
在C++ 11中这样做的惯用方法是什么?