mardi 25 juillet 2017

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

Based off of libstdc++'s implementation of atomic exchange ( http://ift.tt/2tGh462 ):

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?

Aucun commentaire:

Enregistrer un commentaire