Search code examples
multithreadingstoragelocalthread-local-storage

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


my question is why use TLS mechanism instead of just local variables in a thread function? Can you please provide some fine example, or what's the advantage of TLS over local vars? Thank you, Mateusz


Solution

  • 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.