如何在c++11中获取整数线程id
c++11 有可能获取当前线程 id,但它不能转换为整数类型:
cout<<std::this_thread::get_id()<<endl;
输出:139918771783456
cout<<(uint64_t)std::this_thread::get_id()<<endl;
错误:从类型 'std::thread::id' 到类型 'uint64_t' 的转换无效 其他类型也一样: 从类型 'std::thread::id' 到类型 'uint32_t' 的无效转换
我真的不想进行指针转换来获取整数线程 id。有没有一些合理的方法(标准,因为我希望它是便携式的)来做到这一点?
c++11 has a possibility of getting current thread id, but it is not castable to integer type:
cout<<std::this_thread::get_id()<<endl;
output : 139918771783456
cout<<(uint64_t)std::this_thread::get_id()<<endl;
error: invalid cast from type ‘std::thread::id’ to type ‘uint64_t’
same for other types:
invalid cast from type ‘std::thread::id’ to type ‘uint32_t’
I really dont want to do pointer casting to get the integer thread id. Is there some reasonable way(standard because I want it to be portable) to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
您只需要
获取
size_t
即可。来自 cppreference:
You just need to do
to get a
size_t
.From cppreference:
另一个id(想法?^^)是使用stringstreams:
如果你不想在出现问题时出现异常,请使用try catch......
Another id (idea? ^^) would be to use stringstreams:
And use try catch if you don't want an exception in the case things go wrong...
可移植的解决方案是将您自己生成的 ID 传递到线程中。
std::thread::id
类型仅用于比较,而不用于算术(即,正如罐头上所说:一个标识符)。即使它由operator<<
生成的文本表示也是未指定,所以你不能依赖它作为数字的表示。您还可以使用 std::thread::id 值到您自己的 id 的映射,并在线程之间共享此映射(通过适当的同步),而不是直接传递 id。
The portable solution is to pass your own generated IDs into the thread.
The
std::thread::id
type is to be used for comparisons only, not for arithmetic (i.e. as it says on the can: an identifier). Even its text representation produced byoperator<<
is unspecified, so you can't rely on it being the representation of a number.You could also use a map of
std::thread::id
values to your own id, and share this map (with proper synchronization) among the threads, instead of passing the id directly.一种想法是使用线程本地存储来存储变量——无论什么类型,只要它符合线程本地存储的规则——然后使用该变量的地址作为“线程ID”。显然任何算术都没有意义,但它将是一个整型。
对于后代:
pthread_self()
返回一个pid_t
并且是 posix。对于便携式的某些定义来说,这是便携式的。gettid()
,几乎肯定不可移植,但它确实返回一个 GDB 友好的值。One idea would be to use thread local storage to store a variable - doesn't matter what type, so long as it complies with the rules of thread local storage - then to use the address of that variable as your "thread id". Obviously any arithemetic will not be meaningful, but it will be an integral type.
For posterity:
pthread_self()
returns apid_t
and is posix. This is portable for some definition of portable.gettid()
, almost certainly not portable, but it does return a GDB friendly value.不使用 thread::get_id() 的一个关键原因是它在单个程序/进程中不是唯一的。这是因为一旦第一个线程完成,该 id 就可以被第二个线程重用。
这看起来是一个可怕的特性,但它是 c++11 中的。
A key reason not to use thread::get_id() is that it isn't unique for in a single program/process. This is because the id can be reused for a second thread, once the first thread finishes.
This seems like a horrible feature, but its whats in c++11.
这样,应该可以工作:
记住包含库 sstream
In this way, should work:
Remember to include library sstream
另一种选择:
x86 64 位中的 g++ 为该函数生成的代码只是:
即,没有任何同步的单个分支,除了第一次调用该函数之外,将被正确预测。之后只需一次内存访问而无需同步。
Another alternative:
The generated code for this function by g++ in x86 64-bit is just:
I.e. a single branch without any synchronization that will be correctly predicted except for the first time you call the function. After that just a single memory access without synchronization.
thread::native_handle()
返回thread::native_handle_type
,它是long unsigned int
的类型定义。如果线程是默认构造的,则native_handle()返回0。
如果有一个操作系统线程附加到它,则返回值非零(POSIX 上为 pthread_t)。
thread::native_handle()
returnsthread::native_handle_type
, which is a typedef tolong unsigned int
.If thread is default constructed, native_handle() returns 0.
If there is an OS thread attached to it, the return value is non-zero (it is pthread_t on POSIX).
这取决于您想要使用 thread_id 做什么;
您可以使用:
这将在您的进程中生成一个唯一的ID;但有一个限制:如果您启动同一进程的多个实例,并且每个实例都将其线程 ID 写入一个公共文件,则无法保证 thread_id 的唯一性;事实上,很可能会有重叠。
在这种情况下,您可以执行以下操作:
现在可以保证系统范围内唯一的线程 ID。
it depends on what you what you want to use the thread_id for;
you can use:
This will generate a unique id withing you process; but there's a limitation: if you launch several instances of the same process and each one of them writes their thread ids to a common file, the uniqueness of the thread_id is not guaranteed; in fact it's very likely you'll have overlaps.
In this case you can do something like:
now you are guaranteed unique thread ids systemwide.
也许这个解决方案对某人有帮助。第一次在
main()
中调用它。警告:names
无限期增长。Maybe this solution be helpful to someone. Call it a first time im
main()
. Warning:names
grows indefinitely.实际上你也可以通过强制转换来做到这一点:
我比较了 casting、std::hash 和 std::stringstream 一百万次迭代,发现std::hash 是最快的解决方案,时间为1293500ns,而转换仅慢 11 毫秒1384200ns 和 std::stringstream 作为最慢,为 351701200ns。
You actually can do it with casting too:
I compared casting, std::hash and std::stringstream on a million iterations and found that std::hash is the fastest solution with a time of 1293500ns while casting is only 11ms slower with 1384200ns and std::stringstream as the slowest at 351701200ns.
thread::id
只是平台类型的包装器。出于所有意图和目的,它将是一个无符号 32 位整数。因此,继续将其视为无符号 32 位整数。现在,如果有人正在为某个异国平台编写代码,但情况并非如此,那么当您迁移到新平台时对此进行测试。到目前为止提供的答案与这里实际发生的事情相去甚远,它们让我想挖出我的眼睛。
我明白为什么他们想让它成为一种独特的类型,但这只是烦人,我们有更好的事情要做,而不是争论这是否符合标准。
thread::id
is just a wrapper around the platform type. For all intents and purposes it's is going to be an unsigned 32-bit integer. So go ahead and treat it as an unsigned 32-bit integer.Now, if someone is writing code for some exotic platform where this is not the case, then test for this as you move to a new platform. The answers provided thus far are so far removed from what's actually going on here that they make me wanna gouge my eyes out.
I get why they wanted to make it a distinct type but this is just annoying we have better things to do than to argue whether this is in line with the standard or not.