从线程外检索 pthread_create 的参数?

Retrieve pthread_create's arg from outside the thread?

这与How to assign unique ids to threads in a pthread wrapper? and The need for id_callback when in a multithread environment?有关。

当我们需要区分unique个线程时,我们不能使用像pthread_self这样的函数,因为线程id被重用了。在这些问题中,由于计数器潜在的线程 ID 重用,建议使用单调递增计数器来提供唯一 ID。然后通过 arg in pthread_create.

将计数器传递给线程

由于重用问题,我认为我们无法维护外部线程 ID 到唯一 ID 的映射。同一个线程 ID 可以有多个唯一 ID。

我们如何从 线程之外检索传递给 pthread_createarg?它甚至可以检索吗?

I don't think we can maintain a map of external thread ids to unique ids because of the reuse problem. The same thread id could have multiple unique ids.

可以,只要在此映射中只保留与当前运行 个线程对应的外部线程ID。当线程退出时,您将其从映射中删除。

地图的用户显然只关心当前 运行 个线程,因为显然它必须识别它想要的线程的唯一方法是外部线程 ID。