[英]A Unique and Constant Identifier for a pthreads thread?
我假设pthread_t
在给定的线程中在整个生命周期中都保持不变,但是我的实验似乎证明这一假设是错误的。 如果给定线程的ID在其生命周期内未保持恒定,那么我如何存储pthread_t
以便另一个线程可以使用pthread_join
阻塞直到该线程完成?
出于其他原因,对我而言,知道如何为可以往返转换为pthread_t
的线程获取唯一标识符将非常有用。 有办法吗?
那里有很多很棒的信息,但是我很难确定这些问题的有用答案。 我将不胜感激!
编辑:另外,我不确定为什么,但是添加sleep(1)并在每个新线程的前端(在线程的功能内)休眠1秒时,一切似乎都按预期工作。 这可能像吸管一样,但是在新线程启动期间pthread_t
值可能会瞬时改变吗?
您不能依赖pthread_t
是唯一的,但是可以使用pthread_equal()
确定两个线程ID是否引用同一线程。
NAME
pthread_equal -- compare thread IDs
SYNOPSIS
#include <pthread.h>
int
pthread_equal(pthread_t t1, pthread_t t2);
DESCRIPTION
The pthread_equal() function compares the thread IDs t1 and t2.
RETURN VALUES
The pthread_equal() function will return non-zero if the thread IDs t1 and t2
correspond to the same thread. Otherwise, it will return zero.
pthread线程的唯一标识符和常量标识符?
为了确保您启动的每个线程都可以唯一标识:
pthread_create
的void*
有关更多详细信息,请参见此答案: 如何在pthread包装器中为线程分配唯一的ID?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.