Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I was recently made aware that thread local storage is limited on some platforms. For example, the docs for the C++ library boost::thread read:

"Note: There is an implementation specific limit to the number of thread specific storage objects that can be created, and this limit may be small."

I've been searching to try and find out the limits for different platforms, but I haven't been able to find an authoritative table. This is an important question if you're writing a crossplatform app that uses TLS. Linux was the only platform I found information for, in the form of a patch Ingo Monar sent in 2002 to the kernel list adding TLS support, where he mentions, "The number of TLS areas is unlimited, and there is no additional allocation overhead associated with TLS support." Which if still true in 2009 (is it?) is pretty nifty.

But what about Linux today? OS X? Windows? Solaris? Embedded OSes? For OS's that run on multiple architectures does it vary across architectures?

Edit: If you're curious why there might be a limit, consider that the space for thread local storage will be preallocated, so you'll be paying a cost for it on every single thread. Even a small amount in the face of lots of threads can be a problem.

like image 675
Joseph Garvin Avatar asked Sep 22 '09 14:09

Joseph Garvin


People also ask

What is thread local storage used for?

Thread Local Storage (TLS) is the mechanism by which each thread in a given multithreaded process allocates storage for thread-specific data. In standard multithreaded programs, data is shared among all threads of a given process, whereas thread local storage is the mechanism for allocating per-thread data.

What is thread local storage in C?

Thread-local storage ( TLS ) is a mechanism by which variables are allocated such that there is one instance of the variable per extant thread. The runtime model GCC uses to implement this originates in the IA-64 processor-specific ABI, but has since been migrated to other processors as well.

How does thread local storage work in Java?

Threads share the data of the process to which it belongs to. This data sharing provides one of the benefits of multithreaded programming. However, in some circumstances, each thread might need its own copy of certain data.

What is threading local?

Thread-local data is data whose values are thread specific. To manage thread-local data, just create an instance of local (or a subclass) and store attributes on it: mydata = threading.local() mydata.x = 1. The instance's values will be different for separate threads.


2 Answers

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.

like image 185
bdonlan Avatar answered Sep 19 '22 23:09

bdonlan


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.

like image 40
jnoss Avatar answered Sep 19 '22 23:09

jnoss