Stream local storage serves for global variables in the context of a single stream.
Consider this example: you are writing a multi-threaded program to process user requests. Multiple users can initiate requests at the same time; your system uses one thread per user.
When a user request arrives, your system calculates the user from which it came, and creates an instance of the UserPermissions object for that user.
There are several ways to make this object available to your running program. One way is to pass UserPermissions each method that it may need, as well as to each method that directly or indirectly calls a method that it may need. This can be problematic, especially in a context where callbacks are used.
If your program was not multithreaded, you set UserPermissions to a global variable. Unfortunately, you cannot do this, because several user queries can be active at the same time.
This place includes a threaded local storage: a process that creates user permissions, installs a UserPermissions object in the local thread store and leaves it there until the request is processed. Thus, all methods can capture UserPermissions as needed, without having to pass them as method parameters.
dasblinkenlight
source share