why to use Thread Local Storage (TlsAlloc, TlsGetValue, ets) instead of local variables why to use Thread Local Storage (TlsAlloc, TlsGetValue, ets) instead of local variables multithreading multithreading

why to use Thread Local Storage (TlsAlloc, TlsGetValue, ets) instead of local variables


If you can use local variables then do so and you invariably can use locals. Only as a last resort should you use thread local storage which suffers from almost all the same disadvantages as global variables. Although you are looking for a reason to use thread local storage, in fact best practice is to search for ways to avoid it!


TLS is helpful for things like user session context information which is thread specific, but might be used in various unrelated methods. In such situations, TLS is more convenient than passing the information up and down the call stack.