A Tech Log

February 2, 2009

Thread Local Storage and .Net

Filed under: Development — adallow @ 9:48 am
Tags: ,

the below is taken from:

http://www.developerfusion.com/article/4272/net-threading-part-ii/3/

The Thread class and System. Threading namespace also contain some methods and classes for realizing thread local storage. Thread local storage is a manner of storing data in a container that is unique to the thread. Many threads could then use the same named container to store their data without concern of collision. Each thread’s local storage is distinct from another thread’s local storage and is only available in the one thread. Listing 3 shows a small sample using the thread-local-storage methods and classes.

using System;
using System.Threading;

namespace ConsoleApplication11
{
class Class1
{
public void ThreadStart()
{
string str1 = "My Cookie "+
Thread.CurrentThread.GetHashCode();
Console.WriteLine("worker thread: {0}", str1);
LocalDataStoreSlot lds =
Thread.GetNamedDataSlot("COOKIE");
Thread.SetData(lds, str1);
Thread.Sleep(1);
LocalDataStoreSlot lds2 =
Thread.GetNamedDataSlot("COOKIE");
string str2 = "";
str2 = (string) Thread.GetData(lds2);
Console.WriteLine("worker thread: {0}", str2);
}

static void Main(string[] args)
{
string str1 = "My Cookie "+
Thread.CurrentThread.GetHashCode();
Console.WriteLine("main thread: {0}", str1);
LocalDataStoreSlot lds =
Thread.AllocateNamedDataSlot("COOKIE");
Thread.SetData(lds, str1);
Class1 obj = new Class1();
Thread thread = new Thread(
new ThreadStart(obj.ThreadStart));
thread.Start();
Thread.Sleep(1);
LocalDataStoreSlot lds2 =
Thread.GetNamedDataSlot("COOKIE");
string str2 = "";
str2 = (string) Thread.GetData(lds2);
Console.WriteLine("main thread: {0}", str2);
}
}
}

You could also create and start more than one thread and the behavior of the thread local storage becomes more obvious. I have played with Win32 thread-local-storage functions and created my own for portability to UNIX, but I have rarely found them very useful. I strongly believe in stateless computing and thread-local-storage contradicts this belief.

Advertisements

January 27, 2009

Ordered Execution With ThreadPool

Filed under: Development — adallow @ 9:38 pm
Tags: ,

Interesting from:

http://msdn.microsoft.com/en-us/magazine/dd419664.aspx

“Consider a pipeline implementation that obtains parallelism benefits by having multiple stages of the pipeline active at any one time.

For example, you could have a pipeline that reads in data from a file, compresses it, encrypts it, and writes it out to a new file. The compression can be done concurrently with the encryption, but not on the same data at the same time, since the output of one needs to be the input to the other. Rather, the compression routine can compress some data and send it off to the encryption routine to be processed, at which point the compression routine can work on the next piece of data.
Since many compression and encryption algorithms maintain a state that affects how future data is compressed and encrypted, it’s important that ordering is maintained. (Never mind that this example deals with files, and it’d be nice if you could decrypt and decompress the results in order to get back the original with all of the data in the correct order.)
There are several potential solutions. The first solution is simply to dedicate a thread to each component. This DedicatedThread would have a first-in-first-out (FIFO) queue of work items to be executed and a single thread that services that queue. When the component has work to be run, it dumps that work into the queue, and eventually the thread will get around to picking up the work and executing it. Since there’s only one thread, only one item will be run at a time. And as a FIFO queue is being used, the work items will be processed in the order they were generated.”

Blog at WordPress.com.