Click here to Skip to main content
15,896,429 members
Articles / Programming Languages / C++

Thread Local Storage - The C++ Way

Rate me:
Please Sign up or sign in to vote.
4.69/5 (23 votes)
27 Aug 20048 min read 185.4K   3.2K   61  
How to put class objects in TLS with automatic destructor invocation.
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;
 		
		}

	}


}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Austria Austria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions