【发布时间】:2015-05-11 04:25:43
【问题描述】:
这与How to assign unique ids to threads in a pthread wrapper? 和The need for id_callback when in a multithread environment? 有关。
当我们需要区分 unique 线程时,我们不能使用像pthread_self 这样的函数,因为线程 id 被重用。在这些问题中,由于计数器潜在的线程 id 重用,建议使用单调递增的计数器来提供唯一的 id。然后计数器通过pthread_create 中的arg 传递给线程。
由于重用问题,我认为我们无法维护外部线程 ID 到唯一 ID 的映射。同一个线程 ID 可以有多个唯一 ID。
我们如何从线程外部检索传递给pthread_create 的arg?它甚至可以检索吗?
【问题讨论】: