pthread_join_np()--Wait for Thread to End


  Syntax:
 #include <pthread.h>
 int pthread_join_np(pthread_t thread, void **status); 
  Service Program Name: QP0WPTHR

  Default Public Authority: *USE

  Threadsafe: Yes

  Signal Safe: No

The pthread_join_np() function waits for a thread to terminate, then returns the threads exit status, while leaving the data structures of the thread available for a later call to pthread_join(), pthread_join_np(), pthread_detach(), or pthread_extendedjoin_np()

If the status parameter is NULL, the thread's exit status is not returned.

The meaning of the threads exit status (value returned to the status memory location) is determined by the application except for the following conditions:

  1. When the thread has been canceled using pthread_cancel(), the exit status of PTHREAD_CANCELED is made available.
  2. When the thread has been terminated as a result of an unhandled IBM® i exception, operator intervention, or other proprietary IBM i mechanism, the exit status of PTHREAD_EXCEPTION_NP is made available.

Eventually, you should call pthread_join(), pthread_detach(), or pthread_extendedjoin_np() without specifying the leaveThreadAllocated option for every thread that is created joinable (with a detach state of PTHREAD_CREATE_JOINABLE) so that the system can reclaim all resources associated with the thread. Failure to join to or detach joinable threads causes memory and other resource leaks until the process ends.

Note:This function is not portable.


Authorities and Locks

None.


Parameters

thread
(Input) Pthread handle to the target thread

status
(Output) Address of the variable to receive the thread's exit status

Return Value

0
pthread_join_np() was successful.

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

Error Conditions

If pthread_join_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 value specified for the argument is not correct.

[ESRCH]

The thread specified could not be found.


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  okStatus        = 12;

void *threadfunc(void *parm)
{
  printf("Inside secondary thread\n");
  return __VOID(okStatus);
}

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

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

  printf("Create thread using attributes that allow join\n");
  rc = pthread_create(&thread, NULL, threadfunc, NULL);
  checkResults("pthread_create()\n", rc);

  printf("Wait for the thread to exit\n");
  rc = pthread_join_np(thread, &status);
  checkResults("pthread_join_np()\n", rc);
  if (__INT(status) != okStatus) {
    printf("Secondary thread failed\n");
    exit(1);
  }

  printf("With pthread_join_np(), we can join repeatedly\n");
  rc = pthread_join_np(thread, &status);
  checkResults("pthread_join_np()\n", rc);
  if (__INT(status) != okStatus) {
    printf("Secondary thread failed\n");
    exit(1);
  }

  printf("Got secondary thread status as expected\n");
  /* Eventually, we should use pthread_join() or pthread_detach() */
  rc = pthread_detach(thread);
  checkResults("pthread_detach()\n", rc);
 
  printf("Main completed\n");
  return 0;
}

Output:

Enter Testcase - QP0WTEST/TPJOINN0
Create thread using attributes that allow join
Wait for the thread to exit
Inside secondary thread
With pthread_join_np(), we can join repeatedly
Got secondary thread status as expected
Main completed


API introduced: V4R3

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