0

I aspect the following methods distributing threads to multicore processors for the execution of tasks, however, in Windows Task Manager, it showed only utilizing one particular core only. How to make it parallel for those 5 threads?

private void ActivateProcess
{
   ...
   ThreadPool.QueueUserWorkItem(new WaitCallback(Worker1));
   ThreadPool.QueueUserWorkItem(new WaitCallback(Worker2)); 
   ThreadPool.QueueUserWorkItem(new WaitCallback(Worker3));
   ThreadPool.QueueUserWorkItem(new WaitCallback(Worker4));
   ThreadPool.QueueUserWorkItem(new WaitCallback(Worker5));
}

...

private void Worker1(Object stateInfo)
{
    try
    {
        //for (int i = 0; i < _NumberOfLoop; i++)
        Parallel.For(0,(long)_NumberOfLoop, i =>
        {
            if (_ForceToStop)
                throw new System.ArgumentException(
                    "Force stop at WorkerThread 1", "Aborted");

            // Process: Process a tasks
            ...
        });

    }
    catch (Exception ex)
    {
        ...
    }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Fusionmate
  • 679
  • 1
  • 8
  • 18
  • I suppose here http://stackoverflow.com/questions/1718465/optimal-number-of-threads-per-core you will find the answer. – Andrei Schneider Mar 01 '12 at 08:26
  • First simple check - which number of threads do you see the number in the task mananger? The distribution of threads to cores is part of the OS, not the application. – weismat Mar 01 '12 at 08:31
  • Why are you using `Parallel.For` inside the code that already executes in the thread pool? – Tudor Mar 01 '12 at 10:19

1 Answers1

1

Hard to say from this code. You even got 2 layers of parallelism, so "it ought to work".

The answer must be in // Process: Process a tasks - it probably hangs on a common, non shared resource.

If the code in the 'tasks' is independent and CPU intensive you should see all your cores get busy. But when you for instance try to mock it with Sleep() you won't see anything happen in TaskManager.

H H
  • 263,252
  • 30
  • 330
  • 514