Use Thread Local Storage To Pass Thread Specific Data

In an ideal world developers typicallycreate instance variables and access these via interfaces to holdthread specific data. There are times however in a multithreadedapplication where this is not realistic due to a variety of factorsincluding inflexible interfaces, legacy code, and the original overalldesign of the application. The .NET framework provides a mechanism tostore data at a thread level and allows you to access this threadspecific data anywhere this thread exists.

This specific thread level storage is known asthread local storage or TLS for short. The .NET threading namespaceallows .NET developers to use TLS from within their multi-threadedapplications to store data that is unique to each thread.

The common language runtime allocates amulti-slot data store array to each process when it is created. Threadscan use data slots inside these data stores to persist and retrieveinformation at a thread level. The access methods used to put data intothese slots and pull data from them accept and return a type of objectand therefore make the use of these data slots very flexible.

The sample code included demonstrates a simpleapplication that uses these data slots or TLS. We will go over a few ofthe lines to help give you a better understanding of what is happeningwhen we put data on and pull data from TLS.

Sample Application:
The sample codeincludes a console application named TLSSample.exe, which is meant todemonstrate how TLS works. A Manager object is created and begins toloop while calling into the processwork method of an instance of aWorker object. The Worker object then generates a random number, whichrepresents the amount of time the Manager object will sleep beforeagain calling into the Worker object, and places this number on TLS.Next, the manager object pulls this number from TLS and sleeps for thespecified amount of time before calling back into the Worker object.

We will now look more closely at the code thatspecifically deals with TLS in our sample application in an effort tobetter understand how to utilize TLS. We want to be able to access thedata slot by name and therefore we need to allocate a named data slot.The following line allocates a data slot with the name sleeptime.

Thread.AllocateNamedDataSlot("sleeptime");

Now that we have allocated the data slot weneed 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 slotand then places the rndValue variable on this data slot. It isimportant to note that the Thread.SetData method takes two parametersthe second parameter is a type of object.

LocalDataStoreSlot myData;
myData=Thread.GetNamedDataSlot("sleeptime");
Thread.SetData(myData, rndValue);

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 slotwe need to take some similar steps that we took to put the value intothe data slot. The code listed below first gets the named data slot andthen we actually read the data from the named data slot. TheThread.GetData method returns an object so we need to coarse this towhatever data type your application needs, in our case this is aninteger.

LocalDataStoreSlot myTLSValue;
myTLSValue=Thread.GetNamedDataSlot("SleepTime");
int tlsValue=(int)Thread.GetData(myTLSValue);

Finally, we need to free the data slot that we allocated in the beginning of our application.

Thread.FreeNamedDataSlot("sleeptime");

Summary:
Thread local storage allows you tostore data that is unique to a thread and whose value is determined atrun time. This type of storage can be very helpful when dealing with anexisting multithreaded application whose interfaces or original designare too inflexible for passing these values another way.About the Author: Doug Doedens is a senior software consultant livingin San Diego specializing in .NET, Visual Basic 6, Java, and enterprisedevelopment. Doug can be reached at ddoedens@hotmail.com

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!