AAA
AAA

Reputation: 41

What the Differences between prctl(PR_SET_NAME PR_SET_NAME ) and pthread_setname_np()?

I need to set name for some threads.

What are the fifferences between prctl(PR_SET_NAME PR_SET_NAME ) and pthread_setname_np()?

Upvotes: 4

Views: 1497

Answers (2)

flu
flu

Reputation: 556

One difference is that when the thread name is longer than 16 bytes (including NULL terminator), prctl(PR_SET_NAME, ...) will truncate the string for you. Where as pthread_setname_np() will simply fail returning ERANGE error code.

Therefore when switching from prctl to pthread_setname_np, you might want to first truncate the name beforehand. Example below.

const std::string threadName16 = threadName.substr(0, 15);
pthread_setname_np(pthread_self(), threadName16.c_str());

Upvotes: 1

HolyHoratio
HolyHoratio

Reputation: 557

pthread_setname_np(pthread_t pth, char* th_name) sets the thread pth's name to th_name, whereas prctl(PR_SET_NAME, th_name) will set the calling thread's name to be th_name.

In the case that the thread passed to pthread_setname_np() is actually the calling thread, it will simply call prctl() directly.

Upvotes: 4

Related Questions