Multithreading: introducing the thread pool

Published Wed, May 27 2009 19:48

Until now, we’ve been starting threads by creating instances of the System.Threading.Thread class in the samples. Thread creation isn’t cheap! If we’re creating a thread just to run a small task, then the thread’s creation might completely outweigh the advantage of running such a small task.

In most cases, instead of creating new threads, we should simply run those tasks on one of the threads maintained on the thread pool that is automatically created for all managed processes. Even though the most used operation is queuing a task in the thread pool, you can also:

  • use the pool to run a task when IO completes;
  • run a (possible recurrent) task at a specific time;
  • execute a task when a kernel object is signaled.

The global CLR thread pool maintains two “sub-pools”. One is used for IO and the other is used for the remaining possible options mentioned above. By default, there is one pool per process and the IO “sub-pool” will contain a maximum of 1000 threads, while the other “sub-pool” (aka worker pool) contains a maximum of 250 threads.

wow! wtf? so many threads? Yes, that’s the maximum number you’ll get per process.However, when the process starts, there aren’t really any threads on the pool )ie, both “sub-pools” are empty. Threads are created as needed (ie, while new task are queued on the thread pool) until the specified minimum number of threads is reached.

Notice that threads might not be immediately created for work items that are queued on the thread pool. When the minimum number of threads is reached, the thread pool will throttle the creation of other threads (meaning, it won’t immediately create new threads per task that is queued on the thread pool). In these cases, the thread pool will create a new thread as needed from 30 to 30 seconds if all the predefined minimum number of threads are all busy doing work.

When a thread ends its work, it returns to thread pool where it waits for more work. If the current number of waiting threads is greater than the minimum specified, then those additional threads will be terminated.

And that’s it for this post. We’ll be returning to this topic in the next posts.

Filed under: ,

Comments

# LA.NET [EN] said on Thursday, June 04, 2009 8:10 AM

When we started looking at how we could use the thread pool for asynchronous work, I (only!) mentioned

# ASPInsiders said on Thursday, June 04, 2009 8:22 AM

When we started looking at how we could use the thread pool for asynchronous work, I (only!) mentioned

# fightfear said on Thursday, November 26, 2009 10:02 PM

Hi everyone

Sorry to be off topic but I coudn't resist sharing this, hoping it might help some of you.

I am not really new to this forum but never bothered to write anything until now. I have reading around this and that forum to find some solution to my problems. I don't want to bore you guys with my horror story but I would like to say a few words to encourage others.

I use to get panic attacks since my childhood and now I am 26. I was some how living with it due to support from my father and friend. I am shy by nature and find it hard to mix with people and so I have few friends. But around 5 years ago, things turned to worst. I lost my parents in an accident. I then my life was turned upside down. I some how managed to get a boyfriend and I finally felt like coming back to life. I loved her and he too but I guess, I was making his life miserable and so he left, 6 months ago. Ok enough of my sad life.

I actually never lost faith in myself. I was taking medication and I spent quite a lot of time on internet looking for some miracle information. I bought lot of books, CDs and what not. And then, I stumbled upon this course by Mr. Sal. It's called Panic away. Just search for "panicaway dot com" and u will find it. I also tried the famous linden method but former seems to work better for me.

And since then my life is slowly and slowly turning around.

His approach is simple and yet effective. I started reading it around 4 months ago and the last attack that I got was 41 days ago. Earlier, it used to be like 1-2 times each week.I wish that I had found this course an year ago. Anyway. That's it, I would say that you guys so give it a try and if this helps you then my effort of writing this looong post would be worthwhile.

I am now looking forward to a life without fears. Wish me good luck.

<img>trend-fusion.com/.../img>

<img>trend-fusion.com/.../img>

Leave a Comment

(required) 
(required) 
(optional)
(required)