Hacker News new | ask | show | jobs
by jguegant 3782 days ago
As I explained in a commentary: As far as I understand gcc implementation. In a pseudo-code it would be:

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...

1 comments

Yes, that's my understanding of the implementation as well. I was commenting on the fact that you might have two totally unrelated p1 and p2, where (hash(p1) % SIZE_OF_POOL) == (hash(p2) % SIZE_OF_POOL), that will end up sharing the same mutex.
Yes definitely tricky. It might give a heart attack to some high frequency system designers! I will investigate this topic in future: maybe someone can come up with a spin-lock or lock free solution.