相关疑难解决方法(0)

两个unique_ptr <T>的无锁交换

交换两个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中这样做的惯用方法是什么?

c++ lock-free unique-ptr atomic-swap c++11

38
推荐指数
2
解决办法
9808
查看次数

标签 统计

atomic-swap ×1

c++ ×1

c++11 ×1

lock-free ×1

unique-ptr ×1