An introduction to C++'s variadic templates: a thread-safe multi-type map
jguegant.github.io
jguegant.github.io
The syntax is, well, horrible, but the things you can do really help eliminate what would otherwise be redundant code.
Just make sure you comment the hell out of things like this or the people that have to read and maintain your code later will hate you.
Variadic templates add new syntax but they make some previously very tedious things relatively trivial once you understand the syntax.
C++17 will bring some further improvements when writing templates. Concepts lite will be a huge deal but there are a number of other niceties too, some already available in clang.
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...
I suppose it doesn't matter since the concepts proposal was torpedoed. (But that's another story.)