为什么存储到原子 unique_ptr 会导致崩溃?
Why is the store to an atomic unique_ptr causing a crash?
在 i7-4790 处理器 (x86-64) 上,代码在 VC++2013 (v120) 下编译没有问题。
int main()
{
std::atomic<std::unique_ptr<int>> p;
p.store(std::make_unique<int>(5));
}
一次 main()
returns,我崩溃了:
Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
怎么回事?
您不能用 std::unique_ptr
实例化 std::atomic
。 cppreference
std::atomic may be instantiated with any TriviallyCopyable type T. std::atomic is neither copyable nor movable.
并且 std::unique_ptr
不可平凡复制
The class satisfies the requirements of MoveConstructible and MoveAssignable, but not the requirements of either CopyConstructible or CopyAssignable.
你可以使用 std::shared_ptr
that does have free functions defined to allow you to have atomic stores and loads
在 i7-4790 处理器 (x86-64) 上,代码在 VC++2013 (v120) 下编译没有问题。
int main()
{
std::atomic<std::unique_ptr<int>> p;
p.store(std::make_unique<int>(5));
}
一次 main()
returns,我崩溃了:
Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
怎么回事?
您不能用 std::unique_ptr
实例化 std::atomic
。 cppreference
std::atomic may be instantiated with any TriviallyCopyable type T. std::atomic is neither copyable nor movable.
并且 std::unique_ptr
不可平凡复制
The class satisfies the requirements of MoveConstructible and MoveAssignable, but not the requirements of either CopyConstructible or CopyAssignable.
你可以使用 std::shared_ptr
that does have free functions defined to allow you to have atomic stores and loads