Thread-efficient nonce generations
If all you do in your lock is global_counter++ you may as well make it a c++11 atomic, and just write local_counter = global_counter++.

Or, using what you've used so far, local_counter = __sync_fetch_and_add(&amp;global_counter,1)

You may drop the wraparound check. You can increment once per nanosecond and it'd flip in a few hundred years.

After all that's been said in the comments I'm pretty much convinced there are no simple (read: 3 lines of code) solutions given 4g threads will get created and we have only 32 bit atomics.

There are 3 valid solutions:


Stay with the mutex. I guess if you're spending the time to create a thread, it's not the end of the world to lock a mutex once per the thread's lifetime.
Reuse those threads so you never get to 4g. Maybe you can not coz your client's app created them?
Reuse the nonce ranges. Having so many threads means most of them exit way before they consume the 64bit range per 32bit snapshot.
Now, you can have a destructor on the TLS entry of the snapshot. Lookup pthread_key_create.
You can use this destructor to push the current snapshot and counter into a lock free stack and pop them out in get_new_snapshot.
You can init that array/stack with 1000 nonce ranges (999,0), (998,0),... (0,0)
The first thread would pop (0,0). When it exits it'd push back whatever it got to. Say (0,122283).
If a thread ever consumes it's 64bit range of nonces (if your app runs for a millennia) it can pop another range.
You can pre-allocate a stack large enough for as many concurrent threads as you like. If it's ever consumed default to dynamically allocating more ranges. But this would would probably never get executed.


I can give more detail about a pre-allocated lock free stack, if you like.