by deweerdt · 10y ago · 0 comments
A side effect of the shared pool is that you might have two totally unrelated pointers share the same mutex, leading to surprising locking behaviors. Something to keep in mind.
Mutex mutexes[SIZE_OF_POOL];
int hash(void* p) { ... }
std::shared_ptr<t> atomic_load( const std::shared_ptr<t>* p )
{
int r = hash(p);
lock(mutextes[r % SIZE_OF_POOL]);
return *p; // Safe copy of p.
}All the other atomic operations (store, etc) use the same lock pool, so it should be fine for the copy! If you want to take a look at gcc's implementation:
https://github.com/gcc-mirror/gcc/blob/bd3f0a53c07086e978ea4...
https://github.com/gcc-mirror/gcc/blob/bd3f0a53c07086e978ea4...