Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is compare_exchange_weak for std::shared_ptr broken in msvs 2013?

Look at the sample, please

std::atomic < std::shared_ptr < int > > a;

std::shared_ptr < int > b;
std::shared_ptr < int > c = std::make_shared < int > (10);

while(a.compare_exchange_weak(b, c));

assert(a.load() == c);  
assert(a.load().use_count() == 2); // <- assertion is failed.

What do you think? Is it compiler error?

Build with msvs 2013 in win32 mode

like image 608
sliser Avatar asked Dec 15 '22 03:12

sliser


1 Answers

Your program exhibits undefined behavior.

29.5/1 There is a generic class template atomic<T>. The type of the template argument T shall be trivially copyable (3.9).

shared_ptr<int> is not trivially copyable.

like image 194
Igor Tandetnik Avatar answered Dec 27 '22 12:12

Igor Tandetnik