Bak2Basics: All about threading in .NET

Posted on 10/21/2006 @ 7:39 AM in #Bak2Basics by | Feedback | 6589 views

Threading – is one of those topics that I keep reading and keep forgetting about. So I figured, I’d write out a nice little article here for both my benefit, and for anyone else’s benefit that may wish to brush up on threading.

So here we go. Basics first   - What is a thread? A thread is a path of execution inside a process.

In other words, within a process, if you want multiple things done concurrently, you’d put them on a concurrently running threads. Now because your code execution follows multiple paths – things get rather interesting because you literally have to worry about the possibility of threads stepping over each other.

But first – How do you start a thread?

Threading is implemented in the System.Threading namespace. A thread is represented by the System.Threading.Thread class. If you examine the constructor for Thread, it either takes the ParameterizedThreadStart delegate or the ThreadStart delegate. The ThreadStart delegate has the signature of a method that returns a void and takes no parameters. So essentially, you could write out such a method using code like this –

static void DoSomeWork()
{
   
Thread.Sleep(10) ;
   
Console.WriteLine(
     
"Current ThreadID:" + Thread.CurrentThread.ManagedThreadId) ;
}

And then you can fire up 5 threads using the following code:

static void Main(string[] args)
{
   
for (int i = 0; i < 5; i++)
   
{
       
Thread theThread = new Thread(DoSomeWork) ;
       
theThread.Start() ;   
   
}
   
Console.WriteLine("I am done!") ;
   
Console.Read() ;
}

The output of the above looks like this –

Current ThreadID:11
Current ThreadID:12
I am done!
Current ThreadID:14
Current ThreadID:15
Current ThreadID:13

Curiously, the “I am done” or even the order of the threads may differ – because the order is really non-deterministic, all the 5 threads are executing in parallel.

But frequently you may want to have some kind of relative synchronization between these threads. Also, sometimes you may wish to pass parameters to the thread. Let us look at these one by one.

So, How do you pass parameters to your thread?

In .NET 1.x it was tougher, but .NET 2.0 makes it easier. You just use ParameterizedThreadStart as shown below.

static void Main(string[] args)
{
   
for (int i = 0; i < 5; i++)
   
{
       
Thread theThread = new Thread(DoSomeWork) ;
       
theThread.Start("Input Parameter:" + i.ToString()) ;   
   
}
   
Console.WriteLine("I am done!") ;
   
Console.Read() ;
}

static void DoSomeWork(object inputParm)
{
   
Thread.Sleep(10) ;
   
Console.WriteLine(inputParm) ;
   
Console.WriteLine(
     
"Current ThreadID:" + Thread.CurrentThread.ManagedThreadId) ;

}

The output this time looks like this –

I am done!
Input Parameter:0
Current ThreadID:10
Input Parameter:1
Current ThreadID:11
Input Parameter:2
Current ThreadID:12
Input Parameter:4
Current ThreadID:14
Input Parameter:3
Current ThreadID:13

As you can see, the order is still non-deterministic, but hey – you can now pass parameters. How cool is that J.

So the next Q is, how can you achieve some level of synchronization between your threads? This is what I will cover in the next upcoming blogposts.

Waiting for another thread to finish

The problem of re-entrant code

 

Sound off but keep it civil:

Older comments..