pthreads线程的唯一和常量标识符? [英] A Unique and Constant Identifier for a pthreads thread?

查看:185
本文介绍了pthreads线程的唯一和常量标识符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我假设对于一个给定的线程, pthread_t 在整个生命周期内保持不变,但是我的实验似乎证明这个假设是假的。如果给定线程的id在整个生命周期中不保持不变,那么如何存储 pthread_t ,以便另一个线程可以使用 pthread_join 阻塞,直到线程完成?



由于其他原因,我将有用的知道如何获得一个唯一的标识符,我可以转换的线程来回到 pthread_t 。有这样做吗?



有很多很好的信息,但我已经很难确定有用的答案这些问题。 我会感谢任何可以获得的帮助/建议!



编辑:此外,我不知道为什么,正如预期当添加sleep(1)和睡眠1秒在每个新线程的前面(在线程的函数内)。这可能是作为吸管,但是在一个新的线程或某个东西开始时 pthread_t 的值会瞬间改变

pthread_t 是唯一的,但你可以使用 pthread_equal()以确定两个线程ID是否指向同一个线程。

  NAME 
pthread_equal - IDs

概要
#include< pthread.h>

int
pthread_equal(pthread_t t1,pthread_t t2);

DESCRIPTION
pthread_equal()函数比较线程ID t1和t2。

返回值
如果线程ID t1和t2
对应于同一个线程,pthread_equal()函数将返回非零。否则,它将返回零。


I presumed that a pthread_t remains constant - for a given thread - for its entire life, but my experimentation seems to be proving this assumption false. If the id for a given thread does not remain constant across its life, how can I store a pthread_t so another thread can use pthread_join to block until the thread is finished?

For other reasons it would be useful for me to know how to get a unique identifier for a thread that I can convert back and forth to a pthread_t. Is there a way of doing this?

There is lots of great information out there, but I've had a hard time pinning down useful answers for these questions. I'd appreciate any help/advice I can get!

edit: Also, i'm not sure why, but everything seems to work as expected when adding sleep(1) and sleeping for 1sec at the front of every new thread (within the thread's function). This is probably grasping as straws, but could pthread_t values change momentarily during the start of a new thread or something??

解决方案

You cannot rely on a pthread_t being unique, but you can use pthread_equal() to determine whether two thread ids refer to the same thread.

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.

这篇关于pthreads线程的唯一和常量标识符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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