pthread_is_multithreaded_np()--Check Current Number of Threads


  Syntax:
 #include <pthread.h>
 unsigned int pthread_is_multithreaded_np(void);
  Service Program Name: QP0WPTHR

  Default Public Authority: *USE

  Threadsafe: Yes

  Signal Safe: Yes

The pthread_is_multithreaded_np() function returns true or false, indicating whether the current process has more than one thread. A return value of zero indicates that the calling thread is the only thread in the process. A value not equal to zero, indicates that there were multiple other threads in the process at the time of the call to pthread_is_multithreaded_np().

The total number of threads currently in the process can be determined by adding 1 to the return value of pthread_is_multithreaded_np().

Note: This function is not portable.


Authorities and Locks

None.


Parameters

None.


Return Value

0
No other threads exist in the process.

value
There are currently value+1 total threads in the process.

Error Conditions

None.


Related Information


Example

Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.

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

#define         NUMTHREADS    3

void *threadfunc(void *parm)
{
  int           myHiId;
  int           myId;
  pthread_t     me = pthread_self();

  printf("Inside the New Thread\n");
  sleep(2); /* Sleep is not a very robust way to serialize threads */
  return NULL;
}

int main(int argc, char **argv)
{
  pthread_t             thread[NUMTHREADS];
  int                   rc=0;
  int                   theHiId=0;
  int                   theId=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, threadfunc, NULL);
    checkResults("pthread_create()\n", rc);
    printf("Main: Currently %d threads\n",
           pthread_is_multithreaded_np() + 1);
  }

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

  if (rc = pthread_is_multithreaded_np()) {
    printf("Error: %d Threads still exist!\n", rc+1);
    exit(1);
  }
  printf("Main completed\n");
  return 0;
}

Output:

Enter Testcase - QP0WTEST/TPISMT0
Create 3 threads
Main: Currently 2 threads
Main: Currently 3 threads
Main: Currently 4 threads
Join to threads
Inside the New Thread
Inside the New Thread
Inside the New Thread
Main completed


API introduced: V4R3

[ Back to top | Pthread APIs | APIs by category ]