Thread local memory is a small region of thread-specific memory assigned to a thread when it is started.

A thread can allocate thread local memory by calling DosAllocThreadLocalMemory. Thread local memory is freed by calling DosFreeThreadLocalMemory.

The thread local memory region is 32 DWORDS (4 bytes per DWORD) in size, and up to 8 DWORDS can be allocated with each call to DosAllocThreadLocalMemory. To allocate more than 8 DWORDS, a thread must call DosAllocThreadLocalMemory more than once.

Note that thread local memory should be used sparingly. You should not use the entire 128 byte region.

The following code fragment allocates six DWORDS in thread local memory and then frees these six DWORDS.

#define  INCL_DOSPROCESS
#define  INCL_DOSERRORS
#include <os2.h>
#include <stdio.h>          /* Needed for printf */

PULONG   pulMemBlock  = NULL;       /* Pointer to thread DWORDs returned */
APIRET   ulrc         = NO_ERROR;   /* Return code                       */

  ulrc = DosAllocThreadLocalMemory(6,
                                   &pulMemBlock );   /* Allocate 6 DWORDs */

  if (ulrc != NO_ERROR) {
    printf("DosAllocThreadLocalMemory error: return code = %u\n",
           ulrc);
    return 1;
  }

  .
  .
  /* ... Use the thread-local memory block ... */
  .
  .

  ulrc = DosFreeThreadLocalMemory(pulMemBlock);       /* Free the memory block */

  if (ulrc != NO_ERROR) {
    printf("DosFreeThreadLocalMemory error: return code = %u\n",
           ulrc);
    return 1;
  }


[Back] [Next]