mna mna - 3 years ago 84
C++ Question

How is atomic exchange of std:shared_ptr in libstdc++ correct

Based off of libstdc++'s implementation of atomic exchange ( https://gcc.gnu.org/ml/libstdc++/2014-10/msg00154.html ):

It looks like 16 mutexes are statically allocated. When an atomic swap needs to occur, the swapping code hashes the 2 pointers to be swapped to one (or two) of those static mutexes, locks, and swaps. However if a shared_ptr is shared between threads and is being concurrently accessed, how do the mutexes help guarantee synchronization of the concurrent access and modification? I’m guessing the code assumes correct alignment of the internal raw pointer, but that's an x86 rule, not a C++ guarantee. What am I missing that makes the swap atomic and correct without additional locking for each read of the underlying raw pointer?

Answer Source

libstdc++ doesn't have to work on every platform. It only has to work on the platforms it works on.

On the platforms it doesn't work on, it isn't an implementation of the standard library. Use a different one.

So long as people installing and "using" (connecting it to a compiler's environment) libstdc++ (compiler maintainers usually) check their platforms assumptions before using it, there is no problem here.

In general, you cannot implement the std library in a completely platform independent way. You cannot even implement it in a compiler independent way, as some of the things std is required to do cannot be done in C++ without std library support.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download