[Prev] [Contents] [Next]

MCH3402 from pointer returned by pthread_join()

Be sure that no thread returns pointers to items that can be destroyed when a thread terminates. For example, the threads stack is transitory. It only needs to exist for the life of the thread, and may be destroyed when the thread terminates. If you return the address of an automatic variable (or use it as an argument to pthread_exit(), you may experience MCH3402 errors when using it.

Example

#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("Didn't 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




[Prev] [Contents] [Next]
Copyright © 1998, IBM Corporation. All rights reserved.
Comments? Contact
rchthrds@us.ibm.com