Use Thread Local Storage to Pass Thread Specific Data

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

 

using  System;
using  System.Threading;
namespace  TLSSample
{
    
///   <summary>
    
///  This Console Application is meant to show how you can use TLS to store
    
///  Thread specific information.
    
///   </summary>
     class  Parent
    {
        
///   <summary>
        
///  This is the calling object that threads out to other class calls
        
///   </summary>
         

        
static   void  Main( string [] args)
        {
            
//  Step 1
            
//  We need to allocate a named data slot on all threads
            Thread.AllocateNamedDataSlot( " sleeptime " );        

            Manager myManager
= new  Manager();
            
//  Thread out to the other classes
            Thread myThread  = new  Thread( new  ThreadStart(myManager.HandleWorkLoad));
            
            
//  Start the thread here.
            myThread.Start();    
              
            
//  free up the memory on all the threads here
            Thread.FreeNamedDataSlot( " sleeptime " );            
        
        }
    }

    
public   class  Manager
    {
///   <summary>
///  This is meant to be a simple class implementation that calls down into other classe 
///   <summary>
         private  Worker myWorker = new  Worker();
        
        
public   void  HandleWorkLoad()
        {
            
            
//  We begin looping here to simulate some other form of processing            
             for ( int  i = 0 ;i < 30 ;i ++ )
            {            
                
//  Assume that we need to get the amount of time
                
//  the manager sleeps from the Worker
    
    
                
//  The Manager calls the processwork method of the workers
                myWorker.ProcessWork();        
            
                
//  Assuming we have just done some significant processing and
                
//  we are now returning to our calling class and need access to the
                
//  information put on TLS

                
int  managerSleepTime = DetermineSleepTime();
            
                Console.WriteLine(
" Sleep time pulled from TLS  "   +  managerSleepTime);
                Thread.Sleep(managerSleepTime);
            }
        }



        
public   int  DetermineSleepTime()
        {

            
//  We need to pull the value from TLS here
            LocalDataStoreSlot myTLSValue;              
            
            
//  We call GetNamedDataSlot to retrieve the value from TLS
            myTLSValue = Thread.GetNamedDataSlot( " sleeptime " );
            
//  This returns an object so we need to cast it to an int
             int  tlsValue = ( int )Thread.GetData(myTLSValue);            
             
return  tlsValue;

        }


    }

    
public   class  Worker
    {
///   <summary>
///  Meant to represent some lower level abstraction in any system
///   </summary>
         private  Random randomSleepTime  = new  Random(); 
        
private   int  maxValue = 400 ;
         
        
public   bool  ProcessWork()
        {
        
            
//  Assume we are doing whatever work we need to do
            
//  here and then add the following to allow us to 
            
//  also get a sleep time from this method.
            
            
//  Get a random number so we can store different values on all threads
             int  rndValue = randomSleepTime.Next(maxValue);
            LocalDataStoreSlot myData;
            myData
= Thread.GetNamedDataSlot( " sleeptime " );

            
//  Set the named data slot equal to the random number created above
            Thread.SetData(myData,rndValue);        
            
return   true ;
         
        }

    }


}



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.

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

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值