Multithreading / BackgroundWorkers with Quad-Cores (C#)
I'm trying to figure out how to best utiltize a quad-core compu开发者_运维百科ter with the use of backgroundworkers. In particular, do I need to specify to run on multiple cores or does the CRL automatically delegate the threads across different cores? Also, is it possible to run multiple instances of BGWs (I one that already runs an endless loop and passes objects back to the main thread, but I would prefer to have other cores being utilized as well).
I think what makes my project a bit different is that there isn't one or two tasks that are very instensive (that would require the use of parallel programming to split the work), but instead my project requires a number of very long loops to pull data and needs to be constantly running calculations...
Any thoughts would be much appreciated!
Cheers
To parallelize loops the Parallel.For or Parallel.Invoke (depending on what you want to do) features of .NET 4.0 can be used very effectively, and in my experience Parallel.Invoke is the fastest way to go (even faster than manually creating background worker threads).
The Windows thread scheduler distributes ready-to-run threads across cores. It is entirely automatic.
The runtime library will "do the right thing" (mostly). It determines how many cores you have and, attempts to balance the workload accordingly.
Your project isn't all that much different, by the way. Although it seems that most of the parallel programming examples we see have to do with taking a single task and splitting it up so that multiple cores can work on it, there are a lot of application that do the kind of thing you're talking about. For example, I have several applications that run multiple threads in a pipeline fashion. One thread is serving the input queue (reading from a file, decompressing data and placing records in the queue), one thread serves an output queue (takes records from the output, compresses them, and writes to disk), and a "main thread" that reads from the input queue, processes records, and writes to the output queue. The limiting factor here is how fast I can compress and write to disk.
Even though the documentation recommends that BackgroundWorker
be used typically for short tasks, it can work well in such an application so long as you don't have a whole bunch of them running (i.e. you exhaust the thread pool). If you need some long-running threads and also need to use the thread pool a lot, then you probably should consider creating your own non-pool threads (using the System.Threading.Thread
class) for the long-running tasks.
精彩评论