[Prev] [Contents] [Next]

Return values from thread start routines are not integers

Be aware that return values from a thread is defined to be of type `void *'. On some platforms, a void * and an integer are easily interchangeable with no loss of information. Until release v4r2, this wasn't true on the AS/400. The AS/400 enforces more strict pointer rules to both prevent and detect application bugs or a malicious program's behavior. Thus, when converting integers to pointers by a mechanism not directly supported by your compiler, the valid pointer information will be lost, and the pointer will always be set to NULL (regardless of its binary value).

New support put into the system in v4r2 however, allows you to store an integer into a pointer, and still have the pointer be non-NULL. You will never be able to store to, read from, or dereference a pointer created by this mechanism, but it will appear non-NULL.

The macros __INT() and __VOID() are provided to aid in compatibility and allow you to easily store and retrieve integer information in pointer variables even if your compiler does not support the direct typecast. These macros allow explicit conversion from a pointer to an integer and the reverse.

The macros __INT() and __VOID() actually result in a function call.

Example

#define _MULTI_THREADED
#include <pthread.h>
#include <stdio.h>
#include "check.h"

int main(int argc, char **argv)
{
  void *status1 = __VOID(5);
  void *status2 = __VOID(999);

  if (status1 == NULL) {
    printf("Status1 pointer is NULL\n");
  }
  else {
    printf("Status1 pointer is non-NULL\n");
  }

  if (status1 == status2) {
    printf("Both status variables as pointers are equal\n");
  }
  else {
    if (status1 > status2) {
      printf("Status1 is greater than status2\n");
    }
    else {
      if (status1 < status2) {
        printf("Status1 is less then status2\n");
      }
      else {
        printf("The pointers are unordered!\n");
      }
    }
  }

  printf("Pointer values stored in status variables are:\n"
         " status1 = %.8x %.8x %.8x %.8x\n"
         " status2 = %.8x %.8x %.8x %.8x\n",
         status1, status2);
  printf("Integer values stored in status variables are:\n"
         " status1 = %d\n"
         " status2 = %d\n",
         __INT(status1), __INT(status2));
  return;
}

Output

Status1 pointer is non-NULL
Status1 is less then status2
Pointer values stored in status variables are:
 status1 = 80000000 00000000 00008302 00000005
 status2 = 80000000 00000000 00008302 000003e7
Integer values stored in status variables are:
 status1 = 5
 status2 = 999




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