In an ideal world developers typically create instance variables and access these via interfaces to hold thread specific data. There are times however in a multithreaded application where this is not realistic due to a variety of factors including inflexible interfaces, legacy code, and the original overall design of the application. The .NET framework provides a mechanism to store data at a thread level and allows you to access this thread specific data anywhere this thread exists.
This specific thread level storage is known as thread local storage or TLS for short. The .NET threading namespace allows .NET developers to use TLS from within their multi-threaded applications to store data that is unique to each thread.
The common language runtime allocates a multi-slot data store array to each process when it is created. Threads can use data slots inside these data stores to persist and retrieve information at a thread level. The access methods used to put data into these slots and pull data from them accept and return a type of object and therefore make the use of these data slots very flexible.
The sample code included demonstrates a simple application that uses these data slots or TLS. We will go over a few of the lines to help give you a better understanding of what is happening when we put data on and pull data from TLS.
The sample code includes a console application named TLSSample.exe, which is meant to demonstrate how TLS works. A Manager object is created and begins to loop while calling into the processwork method of an instance of a Worker object. The Worker object then generates a random number, which represents the amount of time the Manager object will sleep before again calling into the Worker object, and places this number on TLS. Next, the manager object pulls this number from TLS and sleeps for the specified amount of time before calling back into the Worker object.
We will now look more closely at the code that specifically deals with TLS in our sample application in an effort to better understand how to utilize TLS. We want to be able to access the data slot by name and therefore we need to allocate a named data slot. The following line allocates a data slot with the name sleeptime.
Now that we have allocated the data slot we need to place a thread specific value on it so we can access it later. The code snippet below first gets the thread specific named data slot and then places the rndValue variable on this data slot. It is important to note that the Thread.SetData method takes two parameters the second parameter is a type of object.
Now that we have placed a value on the data slot we need to read it back out from higher up the stack.
In order to read the value from the data slot we need to take some similar steps that we took to put the value into the data slot. The code listed below first gets the named data slot and then we actually read the data from the named data slot. The Thread.GetData method returns an object so we need to coarse this to whatever data type your application needs, in our case this is an integer.
LocalDataStoreSlot myTLSValue; int tlsValue=(int)Thread.GetData(myTLSValue);
Finally, we need to free the data slot that we allocated in the beginning of our application.
Thread local storage allows you to store data that is unique to a thread and whose value is determined at run time. This type of storage can be very helpful when dealing with an existing multithreaded application whose interfaces or original design are too inflexible for passing these values another way.