Eugene Ryabtsev
Eugene Ryabtsev

Reputation: 2301

Name a thread spun off by a library

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?

Upvotes: 0

Views: 21

Answers (1)

Eugene Ryabtsev
Eugene Ryabtsev

Reputation: 2301

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

Upvotes: 1

Related Questions