On which platforms is thread local storage limited and how much is available? On which platforms is thread local storage limited and how much is available? multithreading multithreading

On which platforms is thread local storage limited and how much is available?


On Linux, if you are using __thread TLS data, the only limit is set by your available address space, as this data is simply allocated as regular RAM referenced by the gs (on x86) or fs (on x86-64) segment descriptors. Note that, in some cases, allocation of TLS data used by dynamically loaded libraries can be elided in threads that do not use that TLS data.

TLS allocated by pthread_key_create and friends, however, is limited to PTHREAD_KEYS_MAX slots (this applies to all conforming pthreads implementations).

For more information on the TLS implemenetation on Linux, see ELF Handling For Thread-Local Storage and The Native POSIX Thread Library for Linux.

That said, if you need portability, your best bet is to minimize TLS use - put a single pointer in TLS, and put everything you need in a data structure hung off that pointer.


I have only used TLS on Windows, and there are slight differences between versions in how much can be used:http://msdn.microsoft.com/en-us/library/ms686749(VS.85).aspx

I assume that your code is only targeting operating systems that support threads - in the past I have worked with embedded and desktop OSes that do not support threading, so do not support TLS.


On the Mac, I know of Task-Specific Storage in the Multiprocessing Services API:

MPAllocateTaskStorageIndexMPDeallocateTaskStorageIndexMPGetTaskStorageValueMPSetTaskStorageValue

This looks very similar to Windows thread local storage.

I'm not sure if this API is currently recommended for thread local storage on the Mac. Perhaps there is something newer.