6

I am newbe in multithreading. I have 4 logical processes on my computer and I want to run 4 equal tasks in threads on 4 different cores. How can i do it? I tried to use BackgroundWorker but 4 instances of BackgroundWorker fill only 2 cores of 4 available. My code sample with BackgroundWorker's:

        BackgroundWorker worker = new BackgroundWorker();
        worker.DoWork += new DoWorkEventHandler(worker_DoWork);
        worker.RunWorkerAsync(calculationParams);
        BackgroundWorker worker1 = new BackgroundWorker();
        worker1.DoWork += new DoWorkEventHandler(worker_DoWork);
        worker1.RunWorkerAsync(calculationParams1);

        BackgroundWorker worker2 = new BackgroundWorker();
        worker2.DoWork += new DoWorkEventHandler(worker_DoWork);
        worker2.RunWorkerAsync(calculationParams2);

        BackgroundWorker worker3 = new BackgroundWorker();
        worker3.DoWork += new DoWorkEventHandler(worker_DoWork);
        worker3.RunWorkerAsync(calculationParams3);
Bob Horn
  • 33,387
  • 34
  • 113
  • 219
Frank59
  • 3,141
  • 4
  • 31
  • 53
  • 1
    Why is it important for your functions to run on different cores? – Jens Kloster Nov 10 '13 at 14:53
  • @JensKloster has a good point. Let .NET manage the optimal distribution of threads for you. – Bob Horn Nov 10 '13 at 14:56
  • 2
    One of the links from Igor's answer states: `The TPL scales the degree of concurrency dynamically to efficiently use all the cores that are available. By using TPL, you can maximize the performance of your code while focusing on the work that your program is designed to accomplish.` – Bob Horn Nov 10 '13 at 15:00
  • @Jens Kloster, i need to calculate long tasks and now i see that 50% of my processor is free. i want to reduce time of calculation – Frank59 Nov 10 '13 at 15:00
  • @Frank59 What version of .NET are you using? – Bob Horn Nov 10 '13 at 16:34
  • i am using .NET 4.5. Interesting fact: I tried to use Parallels.ForEach() and all 4 cores worked, but only on <50%. But when i run task without multithreading working only 1 process on 100%. – Frank59 Nov 10 '13 at 17:28
  • 1
    If you're using .NET 4.5, then you should just be using the TPL and not try to manage the threading yourself. Turns out that .NET has done a pretty good job of doing that so you don't have to. – Bob Horn Nov 10 '13 at 17:30
  • i try to use TPL but i see strange process percent values. All 4 cores working but only on 50%. – Frank59 Nov 10 '13 at 17:33
  • Why is that strange? Isn't it possible that 50% is all that is necessary for what you're doing? – Bob Horn Nov 10 '13 at 17:53

2 Answers2

3

You can set processor affinity for a task, if you use tasks. Check out the following post: Force Task<T> to different core ?.

I don't think you can do this with BackgroundWorker. You should use either threads or tasks.

Another post you might find interesting is: Multi core programming using Task Parallel Library with .NET 4.0.

Community
  • 1
  • 1
Igor Ševo
  • 5,459
  • 3
  • 35
  • 80
0

Reason may be in you call logical processes. For example if you have hyperthreading on, using additional logical (but not phycical) cores may give no advantage

Alex Zhukovskiy
  • 9,565
  • 11
  • 75
  • 151
  • i have "hyperthreading on" on my computer and now when i statring tasks execution working all 4 cores but <=50%. It's really strange – Frank59 Nov 10 '13 at 17:31