|
Syntax #include <pthread.h> void pthread_exit(void *status); Threadsafe: Yes Signal Safe: No |
The pthread_exit() function terminates the calling thread, making its exit status available to any waiting threads. Normally, a thread terminates by returning from the start routine that was specified in the pthread_create() call which started it. An implicit call to pthread_exit() occurs when any thread returns from its start routine. (With the exception of the initial thread, at which time an implicit call to exit() occurs). The pthread_exit() function provides an interface similar to exit() but on a per-thread basis.
Note that in the OS/400 implementation of threads, the initial thread is special. Termination of the initial thread via pthread_exit() or any thread termination mechanism terminates the entire process.
The following activities occur in this order when a thread terminates via a return from its start routine or pthread_exit() or thread cancelation:
Cleanup handlers and data destructors are not called when the application calls exit() or abort() or otherwise terminates the process. Cleanup handlers and data destructors are not called when a thread terminates via any proprietary OS/400 mechanism other than the Pthread interfaces.
The meaning of the status parameter is determined by the application except for the following conditions:
If pthread_exit() is called by application code after step 3 in the above list, pthread_exit() will fail with the CPF1F81 exception. This indicates that the thread is already considered terminated by the system, and pthread_exit() cannot continue. If your code does not handle this exception, it will appear as if the call to pthread_exit() was successful.
None.
pthread_exit() does not return.
None.
#define _MULTI_THREADED
#include <pthread.h>
#include <stdio.h>
#include "check.h"
int theStatus=5;
void *threadfunc(void *parm)
{
printf("Inside secondary thread\n");
pthread_exit(__VOID(theStatus));
return __VOID(theStatus); /* Not needed, but this makes the compiler smile */
}
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(thread, &status);
checkResults("pthread_join()\n", rc);
if (__INT(status) != theStatus) {
printf("Secondary thread failed\n");
exit(1);
}
printf("Got secondary thread status as expected\n");
printf("Main completed\n");
return 0;
}
Output
Enter Testcase - QP0WTEST/TPEXIT0 Create thread using attributes that allow join Wait for the thread to exit Inside secondary thread Got secondary thread status as expected Main completed