Be sure that no threads return pointers to items that can be destroyed when a thread terminates. For example, the threads stack is transitory. It needs to exist only for the life of the thread, and it may be destroyed when the thread terminates. If you return the address of an automatic variable or use the address of an automatic variable as an argument to pthread_exit(), you may experience MCH3402 errors when you use the address.
The following example contains code that brings up the MCH3402 error.
See Code disclaimer information for information pertaining to code examples.
#define _MULTI_THREADED #include <pthread.h> #include <stdio.h> #include "check.h" void *threadfunc(void *parm) { int rc = 2; printf("Inside secondary thread, return address of local variable.\n"); return &rc; /* THIS IS AN ERROR! */ /* AT THIS POINT, THE STACK FOR THIS THREAD MAY BE DESTROYED */ } int main(int argc, char **argv) { pthread_t thread; int rc=1; void *status; printf("Enter Testcase - %s\n", argv[0]); printf("Create thread that returns status incorrectly\n"); rc = pthread_create(&thread, NULL, threadfunc, NULL); checkResults("pthread_create()\n", rc); printf("Join to thread\n"); rc = pthread_join(thread, &status); checkResults("pthread_join()\n", rc); printf("Checking results from thread. Expect MCH3402\n"); /* Monitor for the MCH3402 exception in this range */ #pragma exception_handler(TestOk, 0, 0, _C2_ALL, _CTLA_HANDLE_NO_MSG, "MCH3402") rc = *(int *)status; #pragma disable_handler TestFailed: printf("Did not get secondary thread results (exception) as expected!\n"); goto TestComplete; TestOk: /* Control goes here for an MCH3402 exception */ printf("Got an MCH3402 as expected\n"); TestComplete: printf("Main completed\n"); return rc; }
Output
Enter Testcase - QP0WTEST/TPJOIN7 Create thread that returns status incorrectly Join to thread Inside secondary thread, return address of local variable. Checking results from thread. Expect MCH3402 Got an MCH3402 as expected Main completed
Top | Pthread APIs | APIs by category |