boost如何获取线程id?
已经创建了一个线程对象,为boost::thread* pthread = new boost::thread(...);
我想获取这个线程对象的id,
就像win32的函数GetCurrentThreadId返回值一样。
阅读文档发现pthread->get_id()能够返回一个boost::thread::id类型的对象
但是这个对象没有办法将其中的thread id打印出来,有谁知道该怎么办吗?
调试窗口能够看到这个threadid的值,但是无法转为int值
[解决办法]
native_handle()方法可以
boost::thread class has members native_handle_type and native_handle providing access to the underlying native handle.
This native handle can be used to change for example the scheduling.
11
XML to PDF by RenderX XEP XSL-FO Formatter, visit us at http://www.renderx.com/
Thread
In general, it is not safe to use this handle with operations that can conflict with the ones provided by Boost.Thread. An example of
bad usage could be detaching a thread directly as it will not change the internals of the boost::thread instance, so for example
the joinable function will continue to return true, while the native thread is no more joinable.
thread t(fct);
thread::native_handle_type hnd=t.native_handle();
pthread_detach(hnd);
assert(t.joinable());
[解决办法]
thread t(fct);
thread::native_handle_type hnd=t.native_handle();
pthread_detach(hnd);
assert(t.joinable());
不知道你要的是什么值
上面这个值 娶到的就是跟具体平台相关的线程ID
比如在POSIX下 可以直接pthread_detach