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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With