The new C++ has this std::thread type. Works like a charm.
Now I would like to give each thread a name for more easy debugging (like java allows you to).
With pthreads I would do:
pthread_setname_np(pthread_self(), "thread_name");
but how can I do this with c++0x?
I know it uses pthreads underneath on Linux systems, but I would like to make my application portable. Is it possible at all?
A portable way to do this is to maintain a map of names, keyed by the thread’s ID, obtained from
thread::get_id(). Alternatively, as suggested in the comments, you could use athread_localvariable, if you only need to access the name from within the thread.If you didn’t need portability, then you could get the underlying
pthread_tfromthread::native_handle()and do whatever platform-specific shenanigans you like with that. Be aware that the_npon the thread naming functions means “not posix”, so they aren’t guaranteed to be available on all pthreads implementations.