pthread_is_initialthread_np()--Check if Running in the Initial Thread


  Syntax:
 #include <pthread.h>
 #include <sched.h>
 int pthread_is_initialthread_np(void);   
  Threadsafe: Yes

  Signal Safe: Yes

The pthread_is_initialthread_np() function returns true or false, indicating if the current thread is the initial thread of the process. A return value true (non 0) indicates that the calling thread is the initial thread. A return value of false (0) indicates that the calling thread is running in a secondary thread.

Note: This function is not portable.


Authorities and Locks

None.


Parameters

None.


Return Value

0
The calling thread is a secondary thread.
value
The calling thread is the initial thread.

Error Conditions

None.


Related Information


Example

See Code disclaimer information for information pertaining to code examples.

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

#define         NUMTHREADS    1

void *function(void *parm)
{
  printf("Inside the function\n");
  if (pthread_is_initialthread_np()) {
     printf("In the initial thread\n");
  }
  else {
     printf("In a secondary thread\n");
  }
  return NULL;
}

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

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

  printf("Create %d threads\n",  NUMTHREADS);

  for (i=0; i<NUMTHREADS; ++i) {
    rc = pthread_create(&thread[i], NULL, function, NULL);
    checkResults("pthread_create()\n", rc);
    printf("Main: Currently %d threads\n",
           pthread_is_initialthread_np() + 1);
  }

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

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

Output:

Enter Testcase - QP0WTEST/TPISIN0
Create 1 threads
Join to threads
Inside the function
In a secondary thread
Inside the function
In the initial thread
Main completed

API introduced: V4R3
Top | Pthread APIs | APIs by category