Return values from thread start routines are not integers

Return values from a thread are defined to be of type void *. On some platforms, a void * and an integer can be easily interchanged with no loss of information. Until Version 4 Release 2, this was not true on the iSeries. The iSeries enforces stricter 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 is lost, and the pointer is always set to NULL (regardless of its binary value).

New support put into the system in Version 4 Release 2 allows you to store an integer into a pointer, and still have the pointer be non-NULL. You cannot store to, read from, or defer a pointer created by this mechanism, but the pointer appears 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 from an integer to a pointer.

Note: The macros __INT() and __VOID() result in function calls.


Example

The following example shows the correct way to store and retrieve integer information in pointer variables.

See Code disclaimer information for information pertaining to code examples.

#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

Top | Pthread APIs | APIs by category