[Prev] [Contents] [Next]

pthread_is_multithreaded_np()--Check the Current Number of Threads

Syntax

#include <pthread.h>
#include <sched.h>
int pthread_is_multithreaded_np(void);
Threadsafe: Yes
Signal Safe: Yes

The pthread_is_multithreaded_np() function returns true or false, indicating if the current process has more than one thread. A return value of 0 indicates that the calling thread is the only thread in the process. A value not equal to 0, 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().

This function is not portable

Parameters

None.

Authorities and Locks

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

#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 isn't 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



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