C++ 如何从 boost::thread 中检索线程 ID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4548395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:42:55  来源:igfitidea点击:

How to retrieve the thread id from a boost::thread?

c++multithreadingboost

提问by AJG85

I want to be able to identify threads by a simple id when logging so that it is easy to trace the execution of a single thread. With windows using the API GetCurrentThreadId()can achieve what I want. In boost::threadthere is a method get_id()but this doesn't represent an integral value like an integer. This object does have a thread_data member which contains an id which seems to be what I want but the data member is private so can't be accessed.

我希望能够在记录时通过简单的 id 识别线程,以便轻松跟踪单个线程的执行情况。用windows 使用APIGetCurrentThreadId()可以实现我想要的。在boost::thread存在一种方法,get_id()但是这并不表示类似的整数的积分值。该对象确实有一个 thread_data 成员,其中包含一个 id,这似乎是我想要的,但该数据成员是私有的,因此无法访问。

What is the boost way to access the thread id for display or identification purposes?

出于显示或识别目的访问线程 ID 的提升方式是什么?

采纳答案by James McNellis

Boost includes an operator<<(std::ostream&, const boost::thread::id&)overload that can be used to write a thread id to a stream (actually, the overload is a template and will work with any specialization of std::basic_ostream, not just std::ostream).

Boost 包含一个operator<<(std::ostream&, const boost::thread::id&)可用于将线程 id 写入流的重载(实际上,该重载是一个模板,适用于 的任何特化std::basic_ostream,而不仅仅是std::ostream)。

The result of printing the id is likely platform-specific, since different platforms may use different internal representations for thread identifiers.

打印 id 的结果可能是特定于平台的,因为不同的平台可能对线程标识符使用不同的内部表示。

回答by Alvaro Luis Bustamante

Too late, but for users looking for an answer, boost allows to consult the thread id as you said, simply calling the following method:

为时已晚,但对于正在寻找答案的用户,boost 允许按照您所说的查询线程 id,只需调用以下方法:

boost::this_thread::get_id()

This method returns a internal id type from boost, that is not numeric as you want. But you can easily convert this number to, for example, an unsigned long taking into account that the id has as hexadecimal representation. This little function will do de Job:

此方法从 boost 返回一个内部 id 类型,它不是您想要的数字。但是,考虑到 id 具有十六进制表示形式,您可以轻松地将此数字转换为例如 unsigned long。这个小函数可以完成工作:

#include <boost/lexical_cast.hpp>
#include <boost/thread.hpp>

unsigned long getThreadId(){
    std::string threadId = boost::lexical_cast<std::string>(boost::this_thread::get_id());
    unsigned long threadNumber = 0;
    sscanf(threadId.c_str(), "%lx", &threadNumber);
    return threadNumber;
}

void drawThreadId(){
    std::cout << getThreadId() << std::endl;
    boost::this_thread::sleep(boost::posix_time::milliseconds(500));
}

int main() {
    for(int i=0; i<10; i++){
        boost::thread thread = boost::thread(drawThreadId);
    }
    return 0;
}

This will return something like this:

这将返回如下内容:

4491075584
4491612160
4492148736
4492685312
4493221888
4493758464
4494295040
4494831616
4495368192
4495904768

Do not forget link with boost_thread and boost_system.

不要忘记与 boost_thread 和 boost_system 的链接。

Hope this helps!

希望这可以帮助!

回答by Clifford

You need to use the member function boost::thread::native_handle(). It returns a type native_handle_typewhich is an implementation defined alias for a native thread identifier, which can then be used with native thread API functions.

您需要使用成员函数boost::thread::native_handle()。它返回一个类型native_handle_type,它是本机线程标识符的实现定义别名,然后可以与本机线程 API 函数一起使用。