[Prev] [Contents] [Next]

pthread_getthreadid_np()--Retrieve a Unique Id for the Calling Thread

Syntax

#include <pthread.h>
pthread_id_np_t pthread_getthreadid_np(void);
Threadsafe: Yes
Signal Safe: Yes

The pthread_getthreadid_np() function retrieves the unique integral identifier that can be used to identify the calling thread in some context for application debug or tracing support.

In some implementations, the thread ID is equivalent to the pthread_t type. In the OS/400 implementation, the pthread_t is an opaque Pthread handle. For the ability to identify a thread via a thread id (unique number), the pthread_getunique_np() and pthread_getthreadid_np() interfaces are provided.

The OS/400 machine implementation of threads provides a 64bit thread ID. The thread ID is returned as a structure containing the hi and low order 4 bytes of the 64bit ID. This allows applications created by compilers that do not yet support 64bit integral values to effectively use the 64bit thread ID.

If your code requires the unique integer identifier for the calling thread often, or in a loop, the pthread_getthreadid_np() function can provide significant performance improvements over the combination of pthread_self(), and pthread_getunique_np() calls that provide equivalent behavior.

For example:

pthread_id_np_t   tid;
tid = pthread_getthreadid_np();
is significantly faster than these calls, but provides the same behavior.

pthread_id_np_t   tid;
pthread_t         self;
self = pthread_self();
pthread_getunique_np(&self, &tid);
As always, if you are calling any function too often, performance improvements can be gained by storing the results in a variable and or passing to other functions which require the results.

This function is not portable

Parameters

None.

Authorities and Locks

None.

Return Value

The pthread_id_np_t structure identifying the thread

Error Conditions

None.

Related Information

Example

#define _MULTI_THREADED
#include <pthread.h>
#include <stdio.h>
#include "check.h"

#define         NUMTHREADS    3

void *threadfunc(void *parm)
{
  printf("Thread 0x%.8x %.8x started\n", pthread_getthreadid_np());
  return NULL;
}

int main(int argc, char **argv)
{
  pthread_t             thread[NUMTHREADS];
  int                   rc=0;
  pthread_id_np_t       tid;
  int                   i=0;

  printf("Enter Testcase - %s\n", argv[0]);

  printf("Main Thread 0x%.8x %.8x\n", pthread_getthreadid_np());

  printf("Create %d threads using joinable attributes\n",
         NUMTHREADS);
  for (i=0; i<NUMTHREADS; ++i) {
    rc = pthread_create(&thread[i], NULL, threadfunc, NULL);
    checkResults("pthread_create()\n", rc);
    pthread_getunique_np(&thread[i], &tid);
    printf("Created thread 0x%.8x %.8x\n", tid);
  }

  printf("Join to threads\n");
  for (i=0; i<NUMTHREADS; ++i) {
    rc = pthread_join(thread[i], NULL);
    checkResults("pthread_join()\n", rc);
  }

  printf("Main completed\n");
  return 0;
}

Output

Enter Testcase - QP0WTEST/TPGETT0
Main Thread 0x00000000 0000006c
Create 3 threads using joinable attributes
Created thread 0x00000000 0000006d
Thread 0x00000000 0000006d started
Created thread 0x00000000 0000006e
Created thread 0x00000000 0000006f
Join to threads
Thread 0x00000000 0000006f started
Thread 0x00000000 0000006e started
Main completed



[Prev] [Contents] [Next]
Copyright © 1998, IBM Corporation. All rights reserved.
Comments? Contact
rchthrds@us.ibm.com