1

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.

jaks
  • 4,407
  • 9
  • 53
  • 68

3 Answers3

3

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
  • 37,459
  • 12
  • 63
  • 82
  • Here my calling thread uses Join, so it waits until created thread exits, then spawns new thread. – jaks Oct 12 '10 at 16:55
  • @Jai: yes, you must start them all first, then start joining. – Paul Ruane Oct 12 '10 at 16:56
  • 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. – jaks Oct 12 '10 at 17:03
  • @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 Oct 12 '10 at 17:20
2

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
  • 1,923
  • 2
  • 16
  • 27
2

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
  • 333
  • 3
  • 11
  • 1
    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 Oct 12 '10 at 18:27