#include <pthread.h> void pthread_exit(void *status);Service Program Name: QP0WPTHR
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 i5/OS implementation of threads, the initial thread is special. Termination of the initial thread by pthread_exit() or any thread termination mechanism terminates the entire process.
The following activities occur in this order when a thread terminates by a return from its start routine or pthread_exit() or thread cancellation:
Do not call pthread_exit() from a cancellation cleanup handler or destructor function that was called as a result of either an implicit or explicit call to pthread_exit(). If pthread_exit() is called from a cancellation cleanup handler, the new invocation of pthread_exit() will continue cancellation cleanup processing using the next cancellation cleanup handler that was pushed. If pthread_exit() is called from a data destructor, the new invocation of pthread_exit() will skip all subsequent calls to any data destructors (regardless of the number of destructor iterations that have completed), and terminate the thread.
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 by any proprietary i5/OS mechanism other than the Pthread interfaces.
The meaning of the status parameter is determined by the application except for the following conditions:
No address error checking is done on the status parameter. Do not call pthread_exit() with, or return the address of, a variable in a threads automatic storage. This storage will be unavailable after the thread terminates.
Note: 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.
See Code disclaimer information for information pertaining to code examples.
#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
Top | Pthread APIs | APIs by category |