matthewaveryusa
matthewaveryusa

Reputation: 652

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?

Upvotes: 2

Views: 1204

Answers (2)

T.C.
T.C.

Reputation: 137330

The atomic_* functions are only atomic with respect to each other. In other words, the only valid way to concurrently access a shared_ptr being modified by atomic_exchange is via atomic_load, which will take the same mutex (based on the shared_ptr's address) and block until the atomic_exchange completes.

Upvotes: 2

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

Reputation: 275385

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.

Upvotes: 6

Related Questions