Adding an option to set the name of a boost::thread

Has any thought been given to allowing a name to be set on a boost::thread, such that (if the underlying implementation supports it) that name would show up in things like top and ps? In Linux, you can name a thread via the prctl() call and it will show up in top - dandy for debugging which of the bazillion threads in my app has decided to suck up all the CPU. I don't know if Windows supports such a call, and IMHO it would be acceptable if the call did nothing in such a case. Ideally, I'd want to see it as a property, something like: void boost::thread::set_name(std::string name) std::string boost::thread::get_name() const;

On Tue, Nov 06, 2012 at 12:50:23PM -0600, david.hagood@gmail.com wrote:
Has any thought been given to allowing a name to be set on a boost::thread, such that (if the underlying implementation supports it) that name would show up in things like top and ps?
In Linux, you can name a thread via the prctl() call and it will show up in top - dandy for debugging which of the bazillion threads in my app has decided to suck up all the CPU.
I don't know if Windows supports such a call, and IMHO it would be acceptable if the call did nothing in such a case.
Ideally, I'd want to see it as a property, something like:
void boost::thread::set_name(std::string name) std::string boost::thread::get_name() const;
On VC++, the following names a thread. const DWORD MS_VC_EXCEPTION=0x406D1388; inline void SetThreadName(DWORD dwThreadID, char const* threadName) { #pragma pack(push,8) typedef struct tagTHREADNAME_INFO { DWORD dwType; // Must be 0x1000. LPCSTR szName; // Pointer to name (in user addr space). DWORD dwThreadID; // Thread ID (-1=caller thread). DWORD dwFlags; // Reserved for future use, must be zero. } THREADNAME_INFO; #pragma pack(pop) THREADNAME_INFO info; info.dwType = 0x1000; info.szName = threadName; info.dwThreadID = dwThreadID; info.dwFlags = 0; __try { RaiseException( MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info ); } __except(EXCEPTION_EXECUTE_HANDLER) { } } -- Lars Viklund | zao@acc.umu.se

Le 06/11/12 19:50, david.hagood@gmail.com a écrit :
Has any thought been given to allowing a name to be set on a boost::thread, such that (if the underlying implementation supports it) that name would show up in things like top and ps?
In Linux, you can name a thread via the prctl() call and it will show up in top - dandy for debugging which of the bazillion threads in my app has decided to suck up all the CPU.
I don't know if Windows supports such a call, and IMHO it would be acceptable if the call did nothing in such a case.
Ideally, I'd want to see it as a property, something like:
void boost::thread::set_name(std::string name) std::string boost::thread::get_name() const;
Hi, yes this should be useful if there is a portable way to implement it. Please could you create a Trac ticket? I will appreciate a patch that works on linux for you. Best, Vicente

Vicente Botet wrote
Le 06/11/12 19:50,
david.hagood@
a écrit :
Has any thought been given to allowing a name to be set on a boost::thread, such that (if the underlying implementation supports it) that name would show up in things like top and ps?
In Linux, you can name a thread via the prctl() call and it will show up in top - dandy for debugging which of the bazillion threads in my app has decided to suck up all the CPU.
I don't know if Windows supports such a call, and IMHO it would be acceptable if the call did nothing in such a case.
Ideally, I'd want to see it as a property, something like:
void boost::thread::set_name(std::string name) std::string boost::thread::get_name() const;
Hi,
yes this should be useful if there is a portable way to implement it. Please could you create a Trac ticket? I will appreciate a patch that works on linux for you.
HI, I have checked prctl and it seems that this set the name of the current thread, could you confirm? In this case it will be better to associate the setting of the name attribute to the thread_attributes class so that the implementation can set it at the start of the thread. Best, Vicente -- View this message in context: http://boost.2283326.n4.nabble.com/Adding-an-option-to-set-the-name-of-a-boo... Sent from the Boost - Dev mailing list archive at Nabble.com.

Hi, In glibc since 2.12 available new pthread interfaces: 1. pthread_setname_np 2. pthread_getname_np -- Regards, niXman ___________________________________________________ Dual-target(32 & 64-bit) MinGW compilers for 32 and 64-bit Windows: http://sourceforge.net/projects/mingwbuilds/

niXman wrote
Hi,
In glibc since 2.12 available new pthread interfaces: 1. pthread_setname_np 2. pthread_getname_np
Thanks for pointing this. On MacOs these functions can not be used to name a specific thread but the current thread, so my preceding remark seems still valid. Best, Vicente -- View this message in context: http://boost.2283326.n4.nabble.com/Adding-an-option-to-set-the-name-of-a-boo... Sent from the Boost - Dev mailing list archive at Nabble.com.

On 11/07/2012 05:35 AM, Vicente Botet wrote:
niXman wrote
Hi,
In glibc since 2.12 available new pthread interfaces: 1. pthread_setname_np 2. pthread_getname_np
Thanks for pointing this. On MacOs these functions can not be used to name a specific thread but the current thread, so my preceding remark seems still valid.
You could always have the thread set up code handle setting the name in the thread's context before transferring control over to the user supplied function.

david.hagood wrote
On 11/07/2012 05:35 AM, Vicente Botet wrote:
niXman wrote
Hi,
In glibc since 2.12 available new pthread interfaces: 1. pthread_setname_np 2. pthread_getname_np
Thanks for pointing this. On MacOs these functions can not be used to name a specific thread but the current thread, so my preceding remark seems still valid.
You could always have the thread set up code handle setting the name in the thread's context before transferring control over to the user supplied function.
This is exactly what I meant when I said "to associate the setting of the name attribute to the thread_attributes class so that the implementation can set it at the start of the thread. " So that the setting can not be of the thread class as the thread will be already started. Vicente -- View this message in context: http://boost.2283326.n4.nabble.com/Adding-an-option-to-set-the-name-of-a-boo... Sent from the Boost - Dev mailing list archive at Nabble.com.

This is exactly what I meant when I said "to associate the setting of the name attribute to the thread_attributes class so that the implementation can set it at the start of the thread. "
So that the setting can not be of the thread class as the thread will be already started.
First, a silly question: where is thread::attributes documented? I looked at the boost.org pages on the thread library, but never found a good, concise reference for what the members of thread::attributes are. Second: Just having a free function under this_thread to set the name in a portable fashion would be a start - at least prctl and the GLib functions can be called at any time to set the thread name. If there were a void this_thread::set_name(std::string name) that would meet most of my needs. (indeed - in times past I've used something like that to help debugging - change the "name" of the thread to indicate state as the thread operates, so you can easily see where a thread is in its operation just by using top or ps).

Le 07/11/12 18:22, david.hagood@gmail.com a écrit :
This is exactly what I meant when I said "to associate the setting of the name attribute to the thread_attributes class so that the implementation can set it at the start of the thread. "
So that the setting can not be of the thread class as the thread will be already started.
First, a silly question: where is thread::attributes documented? I looked at the boost.org pages on the thread library, but never found a good, concise reference for what the members of thread::attributes are.
Here is the reference documentation http://www.boost.org/doc/libs/1_52_0/doc/html/thread/thread_management.html#...
Second: Just having a free function under this_thread to set the name in a portable fashion would be a start - at least prctl and the GLib functions can be called at any time to set the thread name. If there were a void this_thread::set_name(std::string name) that would meet most of my needs.
This is an plausible alternative, but I guess that others will want to set the name at creation time, isn't it?
(indeed - in times past I've used something like that to help debugging - change the "name" of the thread to indicate state as the thread operates, so you can easily see where a thread is in its operation just by using top or ps).
Good point. This could be useful as well. As already I said, in a preceding post, a patch is welcome ;-) Best, Vicente

2012/11/8 Vicente J. Botet Escriba:
This is an plausible alternative, but I guess that others will want to set the name at creation time, isn't it? No :)
-- Regards, niXman ___________________________________________________ Dual-target(32 & 64-bit) MinGW compilers for 32 and 64-bit Windows: http://sourceforge.net/projects/mingwbuilds/
participants (6)
-
David Hagood
-
david.hagood@gmail.com
-
Lars Viklund
-
niXman
-
Vicente Botet
-
Vicente J. Botet Escriba