The situation is as follows: some time during a program's startup a third-party library is initialized. The initialization routine spawns a thread. It is desirable to name that thread. The obvious ways seem to be unavailable (no provision for naming the thread or getting its pthread id seems to be present in library API; we can have a TID, though). So, how can we name this other thread with minimum fuss?
Asked
Active
Viewed 15 times
1 Answers
1
One solution that works is as follows:
pthread_setname_np(pthread_self(), "SpunOffName");
init_the_library(...);
pthread_setname_np(pthread_self(), "OriginalName");
We just rename the main thread and the name is inherited by whatever thread(s) the library spins off. Then we rename the main thread back to its original name (or any other name we want).

Eugene Ryabtsev
- 2,232
- 1
- 23
- 37
-
However note these APIs are Linux-specific. https://stackoverflow.com/a/7989973/192263 might be a useful guide for going down the "name a thread in a portable fashion" rabbit-hole. – ariels Apr 27 '21 at 06:31