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

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

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

David Hagood
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;





_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

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

Lars Viklund
On Tue, Nov 06, 2012 at 12:50:23PM -0600, [hidden email] 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 | [hidden email]

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

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

Vicente Botet
In reply to this post by David Hagood
Le 06/11/12 19:50, [hidden email] 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

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

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

Vicente Botet
Vicente Botet wrote
Le 06/11/12 19:50, [hidden email] 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
Reply | Threaded
Open this post in threaded view
|

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

niXman
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/

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

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

Vicente Botet
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
Reply | Threaded
Open this post in threaded view
|

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

David Hagood
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.



_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

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

Vicente Botet
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
Reply | Threaded
Open this post in threaded view
|

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

David Hagood

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



_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

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

Vicente Botet
Le 07/11/12 18:22, [hidden email] 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#thread.thread_management.thread.attributes
>
> 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


_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

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

niXman
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/

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

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

Vicente Botet
Le 07/11/12 21:50, niXman a écrit :
> 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 :)
>
>
>
:)

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost