Can the implementation of std::atomic with respect to locks change a program's behavior from correct to incorrect?

c++, multithreading

Solution

In order to have deadlock in a program you need to hold more then one lock simultaneously. Accessing or modifying `std::atomic<T>` variable may acquire lock according to the C++11 standard, but it releases the lock as soon as function call finished and it doesn't call any user defined function while holding a lock, so you can't have a situation where two (or more) mutexes are locked simultaneously; hence no deadlock is possible with `std::atomic` internal lockable objects.

Problem

Are there C++ programs that are correct and deadlock free when `atomic::is_lock_free` returns true, but are undefined or could contain a deadlock when `atomic::is_lock_free` returns false? Given that any lock inside an atomic will be acquired and released under the library's control, I can't imagine how to muck things up, but with multithreading and locks there is usually a way :-)

Original source