17

Is it possible to give a name to a boost::thread so that the debuggers tables and the crash logs can be more readable? How?

3 Answers 3

7

You would need to access the underlying thread primitive and assign a name in a system dependent manner. Debugging and crash logs are inherently system dependent and boost::thread is more about non-system-dependency, i.e. about portability.

It seems ( http://www.boost.org/doc/libs/1_43_0/doc/html/thread.html ) that there is no documented way to access underlying system resources for a boost thread. (But I have never used it myself so I may miss something.)

Edit: (As David writes in the comment) http://www.boost.org/doc/libs/1_43_0/doc/html/thread/thread_management.html#thread.thread_management.thread.nativehandle

5
  • 6
    +1, boost::thread has a native_handle method that can be used to retrieve the native API handle of the thread. You would need to dig into the thread library to know what is the particular type that it returns and the API documentation on how to use that handle to give it a name... Jul 27, 2010 at 9:47
  • 14
    For Linux: stackoverflow.com/questions/778085/…
    – moala
    Jul 27, 2010 at 10:31
  • 9
    For Windows: stackoverflow.com/questions/905876/…
    – moala
    Jul 27, 2010 at 10:31
  • 7
    For Mac OS X: stackoverflow.com/questions/2057960/…
    – moala
    Jul 27, 2010 at 10:36
  • 2
    "... in a system dependent manner" The same goes for doing anything to a thread using the system API e.g. creating it. The point of boost is to wrap these various platform-dependent APIs in a single portable API. As you say, "boost::thread is ... about portability". This is an argument in FAVOR of boost having a "set thread name" method. Jul 31, 2017 at 7:53
4

I'm using boost 1.50.0 on Win32 + VS2010 and thread::native_handle contains number which I didn't manage to pair to anything in system. On the other hand, the thread::get_id() method returns directly windows thread ID in form of a hexadecimal string. Notice that the value returned is platform specific, though. The following code does work under Boost 1.50.0 + Win32 + VS2010. Parts of code reused from msdn

const DWORD MS_VC_EXCEPTION = 0x406D1388;
#pragma pack(push, 8)
typedef struct THREADNAME_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)

void _SetThreadName(DWORD threadId, const char* threadName) {
    THREADNAME_INFO info;
    info.dwType = 0x1000;
    info.szName = threadName;
    info.dwThreadID = threadId;
    info.dwFlags = 0;
    __try {
        RaiseException( MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info );
    }
    __except(EXCEPTION_EXECUTE_HANDLER) {
    }
}
void SetThreadName(boost::thread::id threadId, std::string threadName) {
    // convert string to char*
    const char* cchar = threadName.c_str();
    // convert HEX string to DWORD
    unsigned int dwThreadId;
    std::stringstream ss;
    ss << std::hex << threadId;
    ss >> dwThreadId;
    // set thread name
    _SetThreadName((DWORD)dwThreadId, cchar);
}

Call like this:

boost::thread* thr = new boost::thread(boost::bind(...));
SetThreadName(thr->get_id(), "MyName");
5
  • You're missing the THREADNAME_INFO struct and MS_VC_EXCEPTION. Is there no better way to get the number from the boost::thread::id? eek.
    – David
    Aug 20, 2012 at 21:18
  • Sorry about that, I've no idea how it happened, should be complete now. As for the thread number - I don't know. Boost docs say there is no way at all...
    – Odin
    Aug 21, 2012 at 21:43
  • you can get the thread id by passing GetThreadId (in <Windows.h>) the native handle, which you can get from a boost thread.. From there you can do your RaiseException stuff. GetThreadId is more modern though, you won't be able to run on XP I think. There's also a way to get the private unsigned int out of boost::thread:id if you can't use GetThreadId - but that's seriously evil.
    – David
    Aug 21, 2012 at 21:49
  • yes, as is said - using tha native_handle and GetThreadId I got a number which didn't correspond to an application threadId (though it was a valid system threadid). So that didn't work for me.
    – Odin
    Aug 23, 2012 at 8:52
  • I meant that threadID obtained was pointing to an existing thread, but not one created by my application.
    – Odin
    Aug 23, 2012 at 14:21
0

There is a proposal to add this to boost which has had a slow start: https://github.com/boostorg/thread/issues/84

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.