如何在pthread包装器中为线程分配唯一的ID? [英] How to assign unique ids to threads in a pthread wrapper?

查看:106
本文介绍了如何在pthread包装器中为线程分配唯一的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我正在写一个pthread包装器.我尝试在线程创建阶段为线程分配唯一的ID,以便使执行过程具有确定性.如何让每个线程知道他的唯一ID?

Say I am writing a pthread wrapper. I try to assign unique ids to threads in thread creation phase so that I can make the execution deterministic. How can I let each thread know his unique id?

推荐答案

您不能强迫系统使用您选择的ID.

You cannot force the system to use an id of your chosing.

按照@Lazin的注释:pthread_self()会告诉pthread系统赋予它的ID.

As per @Lazin comment: pthread_self() will tell the pthread the id the system has given it.

如果您想分配自己的id(例如一些小整数),那么显然您可以这样做,并将其与使用pthread_create()void* arg参数(最后一个参数)传递的任何其他args一起传递到pthread中.

If you want to allocate your own id (eg some small integer) then clearly you can do that and pass it into the pthread with any other args passed using the void* arg argument (the last argument) of pthread_create().

这篇关于如何在pthread包装器中为线程分配唯一的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆