The code compiles without issue under VC++2013 (v120) on a i7-4790 Processor (x86-64).
int main()
{
std::atomic<std::unique_ptr<int>> p;
p.store(std::make_unique<int>(5));
}
Once main()
returns, I get a crash:
Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
What's going on?
You cannot instantiate a std::atomic
with a std::unique_ptr
. cppreference
std::atomic may be instantiated with any TriviallyCopyable type T. std::atomic is neither copyable nor movable.
And a std::unique_ptr
is not TriviallyCopyable
The class satisfies the requirements of MoveConstructible and MoveAssignable, but not the requirements of either CopyConstructible or CopyAssignable.
You could use a std::shared_ptr
that does have free functions defined to allow you to have atomic stores and loads
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With