views:

81

answers:

4

Hi,

I have 300 threads which is executing one by one. I use join, so its one-by-one. I want to execute N threads at a time.

Can anyone direct me to a link on creating a thread pool in c# (with N threads). My situation is at a time N threads will execute, the rest of the threads will wait. When one thread finishes execution, one waiting thread will enter into execution.

Any code snippet is appreciated. Thanks a lot.

+3  A: 

Join does not dictate that the threads are run sequentially — it merely makes the current thread wait for the specified thread to finish before continuing.

So if you start 300 threads and then join them all, the 300 threads will run in parallel and the joining thread will complete once the 300 threads are finished.

const int COUNT = 300;

// create and start the threads
var threads = new Thread[COUNT];
for (int index = 0; index < COUNT; index += 1)
{
    threads[index] = new Thread(...);
    threads[index].Start();
}

// now they're running, join them all
for (int index = 0; index < COUNT; index += 1)
{
    threads[index].Join();
}

// we're done

The important part is that you start them all before you start joining, otherwise you will wait for each thread to finish before starting the next, thus then they really would be sequential. I guess this is what you may be doing?

Paul Ruane
Here my calling thread uses Join, so it waits until created thread exits, then spawns new thread.
Jai
@Jai: yes, you must start them all first, then start joining.
Paul Ruane
If I start N threads and join it. It will wait, until all N threads are completed. But what I want is if any one thread is completed, a new job (thread) has to be spawned.
Jai
@Jai: Then can't you get the threads themselves to spawn the new jobs, just before they finish? Or, better still, just do the work as part of their existing workload?
Paul Ruane
+2  A: 

If most of your threads are waiting you should have a look at the System.Threading.ThreadPool class. It might just do exactly what you want. And it's efficient.

BaBu
A: 

You can also use Task in .net 4 see this, there are some related articles and u can see how manipulate tasks. there is no need to concern about dispose, in simple threading approach management of threads is your duty. Also threadpool is deprecated don't use it.

SaeedAlg
Thanks. But due to licensing cost I am ended up with VS 2008 and .net 3.5 :). Any help?
Jai
u can use .net 4 vs2008, no problem its free, just change ur application setting to use .net framework 4, (first download and install it :)).
SaeedAlg
Nope. VS2008 wont support .Net 4. Pls see this http://stackoverflow.com/questions/1986287/visual-studio-2008-support-for-new-net-4 and http://stackoverflow.com/questions/1836410/can-i-develop-for-net-framework-4-in-visual-studio-2008
Jai
yes, u can use dlls of .net 4 as refrences, but i think forgot about it is better, bad thing:(.
SaeedAlg
+2  A: 

I think you are probably looking for ThreadPool.QueueUserWorkItem. Using the threadpool reduces the large overhead of thread creation and destruction. You can tune the threads (link in my comments) based on the hardware available, or allow the system to best manage the min/max simultaneous threads. If this is a web app, you can set this in your web.config to set your "N" thread number:

   <system.web>
        <processModel minWorkerThreads="50"/>
   </system.web>

If you are using .NET 4.0 you can also use the "Parallel.ForEach" which will automatically schedule each iteration of a loop onto a multiple threads in the thread pool. (link in my comments)

David Storfer
http://thejoyofcode.com/Tuning_the_ThreadPool.aspx
David Storfer
http://msdn.microsoft.com/en-us/library/dd460720.aspx
David Storfer
Hey one more thing - I just got today's MSDN Flash email and there is a free downloadable e-book on C# Threading -- http://www.albahari.com/threading/
David Storfer
Thanks a lot David :)
Jai