W8136 Initialization of TLS data is not supported on this platform (C++)

From RAD Studio
Jump to: navigation, search

Go Up to Compiler Errors And Warnings (C++) Index



On the macOS platform, for example, TLS (thread local storage) variables are zeroed and cannot be initialized to anything else.

You must initialize the TLS variables at run time. Each thread, including the main thread, must initialize the TLS variables if these are expected to have non-zero values, as shown in the following example:

#include <stdio.h>
#include <string.h>
#include <process.h>
 
int   __declspec(thread) iVal   = 1000;
char  __declspec(thread) sVal[] = "ABCDEFG";
float __declspec(thread) fVal   = 2.5;
 
#if defined(__APPLE__)
static void initTLSVars()
{
  iVal = 1000;
  strcpy(sVal, "ABCDEF");
  fVal = 2.5;
}
#endif
 
static void printTLSVars()
{
  printf("Thread Id:(%08X) - ", __threadid());
  printf("iVal='%d', sVal='%s', fVal='%f'\n", iVal, sVal, fVal);
}
 
void threadHandler(void* /*data*/)
{
  // TLS must be explicitly initialized on macOS
#if defined(__APPLE__)
  initTLSVars();
#endif  
  printTLSVars();
}
 
bool runThreads()
{
#if defined(__APPLE__)
  __borland_use_pthreads();
#endif  
  _beginthread(&threadHandler, 0, 0);
  _beginthread(&threadHandler, 0, 0);
  return true;
}
 
int main()
{
#if defined(__APPLE__)
  initTLSVars();
#endif  
  printTLSVars();
  runThreads();
  // Wait for input before terminating
  getchar();
  return 0;
}

Without the calls to initTLSVars() for the Mac, the program would print zeros for the three thread local storage variables.

On Windows, it is not necessary to call initTLSVars(), because the OS provides built-in TLS support.

This warning is enabled by default. To disable this warning, pass -w-itl or -w-8136 to the compiler.

See Also