Click here to Skip to main content
15,867,141 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I have a question.
If I use a backgroungworker provided in .Net to do some intensive work, will it run on on the same core (I have a quad-core machine) as the parent thread (main thread that created this background worker)?
If I make about 5-10 background workers will they start on different cores .. ?
will they all work as truly parallel ?
will they help the application utilize the multi core feature of the processor at all?
a backgroundworker is started on a new thread (right ?) , so is it any differnet from using thread pool and using parallel.For and parallel.Invoke ?
I need to clear up these concepts.
Posted
Comments
Sergey Alexandrovich Kryukov 22-Sep-13 11:54am    
BackgroundWorker (thread) has nothing to do with Parallel. You should rather tag it as "multithreading".
—SA

Your questions, in order:
Maybe.
Possibly.
Probably not.
Yes.
Yes.
Yes.

If you have N cores, then N threads can truly run in parallel (provided they do not want to access the same resources, including memory objects). However, not all the threads in the system are yours - yours will "get a go" when there are no higher-priority threads waiting and when the system decides to run them (it does time-slice to give them a go, but the execution order is up to it)
As a result, a Background worker may execute on the same core as the main thread, but not at the same time. If you have N+1 threads in the system to run, and N cores, one thread will have to wait until either one of the other threads stops, or waits foe a resource, or the system decides it has had "it's go" and starts the extra thread. Adding more threads than processors can slow the overall execution, as there is an overhead in starting and swapping threads.

A Background worker uses the same mechanism as Thread Pool and Parallel.For, it's just simpler to use, but as a result not as precisely controllable as the Thread Pool, or as specifically limited as Parallel.For
 
Share this answer
 
Comments
ridoy 22-Sep-13 6:14am    
5ed.
Volynsky Alex 22-Sep-13 16:47pm    
Nice answer
First of all, you need to create multiple threads only if your need parallel independent processing logic dictated by the semantic of your application. If you hope to improve performance using threads, you well make it only worse if the number of threads exceed the number of cores. Remember that multithreading incurs some considerable overhead.

Now, about "truly parallel". The threads use the cores they use. However, you can affect the use of the cores using thread and process affinity. Please see my past answer:
How to program multicore[^].

—SA
 
Share this answer
 
See Solution 1 of Database connection every 1000 milliseconds by Timer interval problem[^]

When I originally developed this application, it accessed nine different servers one at a time using Web Services to gather data. That took about ten minutes. When I changed my software so that it created nine threads that each retrieve the data from one of the nine servers, the application ran in less than 1.5 minutes. I still don't know how many cores the user's PC has. I never looked. It didn't seem to matter. They were pleased with the result!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900