Example: Ending a thread in a Pthread program
This example shows how to end a thread in a Pthread program.
Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.
/*
Filename: ATEST12.QCSRC
The output of this example is as follows:
Enter Testcase - LIBRARY/ATEST12
Create/start a thread
Wait for the thread to complete, and release its resources
Thread: End with success
Check the thread status
Main completed
*/
#define _MULTI_THREADED
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define checkResults(string, val) { \
if (val) { \
printf("Failed with %d at %s", val, string); \
exit(1); \
} \
}
const int THREADFAIL = 1;
const int THREADPASS = 0;
void *theThread(void *parm)
{
printf("Thread: End with success\n");
pthread_exit(__VOID(THREADPASS));
printf("Thread: Did not expect to get here!\n");
return __VOID(THREADFAIL);
}
int main(int argc, char **argv)
{
pthread_t thread;
int rc=0;
void *status;
printf("Enter Testcase - %s\n", argv[0]);
printf("Create/start a thread\n");
rc = pthread_create(&thread, NULL, theThread, NULL);
checkResults("pthread_create()\n", rc);
printf("Wait for the thread to complete, and release its resources\n");
rc = pthread_join(thread, &status);
checkResults("pthread_join()\n", rc);
printf("Check the thread status\n");
if (__INT(status) != THREADPASS) {
printf("The thread failed\n");
}
printf("Main completed\n");
return 0;
}