Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split long tasks on different cores?

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);
like image 656
Frank59 Avatar asked Nov 10 '13 14:11

Frank59


1 Answers

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.

like image 166
Igor Ševo Avatar answered Oct 06 '22 16:10

Igor Ševo