Click here to Skip to main content
Click here to Skip to main content
Go to top

Use Thread Local Storage To Pass Thread Specific Data

, 1 Apr 2003
Rate this:
Please Sign up or sign in to vote.
Use named data slots in threads to pass thread-specific data.

Introduction

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.

Sample Application

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 process work 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.

Thread.AllocateNamedDataSlot("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 parameter. The 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 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 int.

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Doug Doedens

United States United States
Doug Doedens is a senior software architect specializing in consulting on enterprise applications and is living in San Diego.

Comments and Discussions

 
GeneralTLS is an old thing Pinmemberdatenkabel11-Nov-06 0:09 
GeneralSee also: Thread-static variables PinmemberDan7212-Apr-03 9:53 
GeneralRe: See also: Thread-static variables PinmemberThomas Lykke Petersen27-Nov-07 19:27 
AnswerRe: See also: Thread-static variables PinmemberBertrand Jobert2-Mar-09 5:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140922.1 | Last Updated 2 Apr 2003
Article Copyright 2003 by Doug Doedens
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid