views:

174

answers:

1

For maintainability purposes, my impulse is to simply use parallel processing all the time. For simple loops it sounds like there is overhead which could actually slow the function, and presumably for loops where order of execution makes a difference it is a mistake.

How do you decide when to use parallel processing and when to avoid it?

+3  A: 

Things that would cause problems:

  • dependency on things with thread affinity; UI controls; asp.net context
  • anything with thread-specific state (thread-static / thread-local)
  • anything with mutable state and side-effects

But more importantly; do you need it? In many cases it is overkill, and threading always has a slight overhead (the total CPU time increases, even if the elapsed time decreases). Now; if you already in a highly threaded environment (such as a busy web-server), this isn't going to help!

There are a couple of scenarios where it comes into its own, usually when the elapsed time is more important than the overall CPU time - so very useful at the client, or on some web-server scenarios. It is especially useful if the state is immutable, so it is pretty trivial to tear the work apart and put the result back together again. There are some great examples of processing fractals etc, or other CPU-intensive work, with Parallel.

Marc Gravell
What is an example of a web server scenario where it would be useful?
Gene
It would be a pretty handy way of writing code that talks to multiple external sources (perhaps hitting "bing", "google", etc all at once) i.e. where CPU isn't the bottleneck, but network latency is. Or for CPU-intensive work if you don't mind impacting other requests.
Marc Gravell