How to split long tasks on different cores?

I am new to 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 using BackgroundWorker , but 4 instances of BackgroundWorker populate only 2 of the 4 available cores. My sample code 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); 
+6
source share
2 answers

You can adjust the proximity of the processor to the task if you use tasks. Check the following message: Force Task <T> to another kernel? .

I do not think you can do this with BackgroundWorker . You must use either threads or tasks.

Another message you might find interesting: Multi-core programming using a parallel task library with .NET 4.0 .

+3
source

In your case, a call to logical processes may appear. For example, if you have hyperthreading on , using additional logical (but not phycical) kernels may not be beneficial.

0
source

Source: https://habr.com/ru/post/957773/


All Articles