pthread_test_exit_np()--Test Thread Exit Status


  Syntax:
 #include <pthread.h>
 #include <sched.h>
 int pthread_test_exit_np(void **status);  
  Service Program Name: QP0WPTHR

  Default Public Authority: *USE

  Threadsafe: Yes

  Signal Safe: Yes

The pthread_test_exit_np() function returns the current state of the thread along with its exit status.

If the thread is currently processing an exit condition due to a call to pthread_exit() or cancellation due to being the target of a pthread_cancel(), pthread_test_exit_np() returns PTHREAD_STATUS_EXIT_NP and sets the exit status pointed to by the status parameter to be the current thread exit status.

If the thread is currently running and is not running cancellation cleanup handlers or data destructors while terminating, pthread_test_exit_np() returns PTHREAD_STATUS_ACTIVE_NP, and does not return the exit status.

Note: This function is not portable.


Authorities and Locks

None.


Parameters

status
Pointer to the parameter to receive the exit status if PTHREAD_STATUS_EXIT_NP is returned

Return Value

PTHREAD_STATUS_ACTIVE_NP
The thread is currently not exiting.

PTHREAD_STATUS_EXIT_NP
The thread is currently exiting.

value
pthread_test_exit_np() was not successful. value is set to indicate the error condition.

Error Conditions

If pthread_test_exit_np() was not successful, the error condition returned usually indicates one of the following errors. Under some conditions, the value returned could indicate an error other than those listed here.

[EINVAL]

The values specified for the argument are not correct.


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"

int     checkStatusFailed1=0;
int     missedHandler1=1;

int     thread1Status=42;

void cleanupHandler1(void *arg)
{
  int           rc;
  void         *status;
  printf("Thread 1 - cleanup handler\n");
  missedHandler1=0;
  rc = pthread_test_exit_np(&status);
  if (rc != PTHREAD_STATUS_EXIT_NP) {
    printf("Thread 1 - returned %d instead "
           "of PTHREAD_STATUS_EXIT_NP\n", rc);
    checkStatusFailed1 = 1;
    return;
  }
  if (__INT(status) != thread1Status) {
    printf("Thread 1 - status = %d\n"
           "Thread 1 - expected %d\n",
           __INT(status), thread1Status);
    checkStatusFailed1=1;
  }
  printf("Thread 1 - correctly got PTHREAD_STATUS_EXIT_NP "
         "and thread exit status of %d\n", thread1Status);
}

void *thread1func(void *parm)
{
  printf("Thread 1 - Entered\n");
  pthread_cleanup_push(cleanupHandler1, NULL);
  pthread_exit(__VOID(thread1Status));
  pthread_cleanup_pop(0);
  return __VOID(0);
}

int main(int argc, char **argv)
{
  pthread_t             thread;
  int                   rc=0;
  void                 *status;

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

  rc = pthread_test_exit_np(&status);
  if (rc != PTHREAD_STATUS_ACTIVE_NP) {
    printf("We should always be in an ACTIVE status here! rc=%d\n",
           rc);
    exit(1);
  }
 
  printf("Create the pthread_exit thread\n");
  rc = pthread_create(&thread, NULL, thread1func, NULL);
  checkResults("pthread_create()\n", rc);
 
  rc = pthread_join(thread, &status);
  checkResults("pthread_join()\n", rc);
  if (__INT(status) != thread1Status) {
    printf("Wrong status from thread 1\n");
  }
 
  if (checkStatusFailed1 ||  missedHandler1) {
    printf("The thread did not complete its test correctly! "
           " check=%d, missed=%d\n",
           checkStatusFailed1, missedHandler1);
    exit(1);
 
  }
  printf("Main completed\n");
  return 0;
}

Output:

Enter Testcase - QP0WTEST/TPTEXIT0
Create the pthread_exit thread
Thread 1 - Entered
Thread 1 - cleanup handler
Thread 1 - correctly got PTHREAD_STATUS_EXIT_NP and thread exit status of 42
Main completed

API introduced: V4R3

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