All About Threads in .Net

0.00 avg. rating (0% score) - 0 votes

Thread management has always been tricky especially from synchronization, locking and resource sharing perspective. At the same time avoiding dead locks is cumbersome. There are quite much features introduced in .Net that are available built-in to reduce effort required writing code from scratch. Not finding the material consolidated at one place, I decided to blog on them. I will be discussing those features one by one here:

Dot Net Thread Pool

Dot Net has provided a thread pool that can be used to execute our requests in multiple thread. It is quite a handy thread pool but it just provides basic functionality. What do I mean by that? I mean it just provides a queuing mechanism to queue up threads and executes these in a queue fashion. No other facility is provided like pause a thread, terminate a thread and most importantly prioritize a thread. Here is how you may use a Thread Pool:

thread pool

In the above code, you are telling the threadpool to execute DisplayMessage method in a new thread. You should use ThreadPool.SetMaxThreads() and ThreadPool.SetMinThreads() to set the minimum and maximum limits for the threads.

If you would like to use smart thread pool, look at Smart ThreadPool available at:

http://www.codeproject.com/KB/threads/smartthreadpool.aspx

Latest Smart Thread Pool can be downloaded from: (thanks Simon)

http://www.codeplex.com/smartthreadpool

Asynchronous Method Calling

Often, it is required to call a method asynchronously. In order to achieve that we need to define delegates. Find below a snippet:

Asynch Call

Monitors

Have you ever heard about monitors. No ? No problem. In multi-threaded applications, the biggest challenge is to avoid execution of multiple threads on the same resource. One thing that could be used for that purpose is Monitor. Look at the following code snippet:

Monitor

It’s been a nice synchronization technique available in .Net. Technically each object has a Monitor associated with it. We can also use Monitor without getting a lock on the shared resource.

NOTE: Just one suggestion, Monitor should be used almost always to make sure resource locking before performing operations on it.

Something About Mutex

We need to coordinate the activities of multiple threads (possibly across process boundaries) to
ensure the efficient use of shared resources or to ensure several threads are not updating the same
shared resource at the same time. Lets take a look at the following snippet using Mutex:

Mutex

Semaphores

There are some business scenarios in which we need to limit the number of threads that can share concurrently some resource. We can do so by using Semaphore class provided by Dot Net. Look at the following code snippet, how we may achieve this:

Semaphore

Confused About Monitor, Mutex and Semaphore:

Monitor should be used in most of the circumstances to make sure resource is locked. Mutex is used for Inter-Process communication. Semaphores are used if a resource lock can be shared by limited number of threads.

0.00 avg. rating (0% score) - 0 votes

W@rfi

Owner of this blog site. Have expertise on Microsoft technologies.

You may also like...

4 Responses

  1. SimonC says:

    U might want to add a link to where to get the latest smartthreadpool
    http://www.codeplex.com/smartthreadpool

  2. admin says:

    Thanks Simon for providing the latest link for the project, I have added it to the post as well.

  3. trishioth says:

    Excellent site http://www.warfiblog.com and I am really pleased to see you have what I am actually looking for here and this this post is exactly what I am interested in. It’s taken me literally 1 hours and 07 minutes of searching the web to find you (just kidding!) so I shall be pleased to become a regular visitor 🙂

  4. admin says:

    @trishioth
    Thanks for your comments. Its really encouraging, keep visiting my blog to find out items. You can also subscribe to receive an email as soon as there is a new post.

Leave a Reply

Your email address will not be published. Required fields are marked *