相关疑难解决方法(0)

是否有无效的pthread_t id?

我想为给定的线程id调用pthread_join,但前提是该线程已经启动.安全的解决方案可能是添加一个变量来跟踪哪个线程在哪里开始.但是,我想知道是否可以检查pthread_t变量,如下面的代码.

pthread_t thr1 = some_invalid_value; //0 ?
pthread_t thr2 = some_invalid_value;

/* thread 1 and 2 are strated or not depending on various condition */
....

/* cleanup */
if(thr1 != some_invalid_value)
    pthread_join(&thr1);

if(thr2 != some_invalid_value)
    pthread_join(&thr2);
Run Code Online (Sandbox Code Playgroud)

其中some_invalid_value可以是0,或者是依赖于实现的'PTHREAD_INVALID_ID'宏

PS:我的假设是pthread_t类型具有可比性和可分配性,基于的假设

PPS:我想这样做,因为我认为在无效的线程id上调用pthread_join是未定义的行为.它不是.但是,加入以前加入的线程是未定义的行为.现在让我们假设重复调用上面的"函数".无条件地调用pthread_join并检查结果可能会导致在先前连接的线程上调用pthread_join.

linux pthreads

51
推荐指数
2
解决办法
2万
查看次数

标签 统计

linux ×1

pthreads ×1