masaya
masaya

Reputation: 490

What is thread ID of POSIX on mac osx

I just read IEEE Std 1003.1™, 2004 Edition (POSIX spec.) to make sure what is actual thread ID on mac osx. In RATIONALE section of pthread_equal() of POSIX SPEC says:

Implementation may choose to define a thread ID as a structure. This allows additional flexibility and robustness over using an int. for example a thread ID could include a sequence number that allows detection of "dangling IDs" copies of a thread ID that has been detected). Since the C language does not support comparison on structure typs the pthread_equal() function is provided to compare thread IDs.

In Mac OSX, pthread_t is defined as a pointer to the structure named '_opaque_pthread_t' which has 3 members. __sig which is type long, __cleanup_stack which is a pointer to a structure, and __opaque which is array of type char.

My first questions are:

  1. pthread_t is used to identify a thread. and it is a pointer to type '_opaque_pthread_t' in osx. So, Is this correct to say that the address of type _opaque_pthread_t represents Thread ID?
  2. What is its member named __sig, __cleanup_stack, and __opaque? Is it OK I think it doesn't matter what it is as application developer?

To find out an answer for my question, I googled and then I found this question:Mac/iPhone: Is there a way to get a thread identifier without using Objective-C? but it raises another question.

Another question is:

  1. mach_port_t is not thread ID. It is an unsigned integer type which represents a port name in GNU Mach. They are different in the meaning. but when I want to get just a unique identifier of thread. Are mach_port_t and pthread_t exchangeable at all times?

Upvotes: 4

Views: 4194

Answers (1)

Vishal
Vishal

Reputation: 636

On OS X pthread_threadid_np, defined in pthread.h, can be used. Note the _np suffix suggests that it is an extension to POSIX.

Upvotes: 4

Related Questions