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?
Boost. Thread is the library that allows you to use threads. Furthermore, it provides classes to synchronize access on data which is shared by multiple threads. Threads have been supported by the standard library since C++11.
Thread IDs Each running thread of execution has a unique ID obtainable from the corresponding boost::thread by calling the get_id() member function, or by calling boost::this_thread::get_id() from within the thread.
Fiber provides a framework for micro-/userland-threads (fibers) scheduled cooperatively. The API contains classes and functions to manage and synchronize fibers similiarly to standard thread support library. Each fiber has its own stack.
A thread can also be detached by explicitly invoking the detach() member function on the boost::thread object. In this case, the boost::thread object ceases to represent the now-detached thread, and instead represents Not-a-Thread.
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
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With