views:

157

answers:

4

I have a set of operations that are very expensive but are all pretty much self-contained. Some of them rely on some "global" states or data, but all very much read-only. The operations themselves, I'm fairly certain, can all be done in parallel, but all the operations need to complete before the program progresses past a certain point.

Is it worth it to add the extra work and danger of multithreading here, especially since the main thread will have to block and wait anyway?

The other issues is that this application will most likely have to run on both Windows and Linux and I'm not sure it's worth the complexity of adding code that uses two different threading systems.

+5  A: 

Well, the first question is: do you actually have a problem?

It sounds like you could certainly parallelize this reasonably safely and efficiently, but if it's not actually a problem to wait for the operations to execute in serial, it may not be worth worrying about.

If this is a batch application which executes overnight, for instance, it's probably not worth doing. If, however, this is a user-facing application and users are getting fed up of waiting, it sounds like it would be worth using multi-threading to solve the issue.

Jon Skeet
+1: Don't add extra complexity where it isn't required. The additional maintenance of threaded code might not be worth the minor performance gains.
Jon Tackabury
Premature optimization is the root of all evil -- Donald Knuth
MattH
Thank you, but it is actually a bit of a problem, or I would not have asked. I am soliciting opinions because threads scare me a little bit and I wondered if others thought that it is worth using them in this scenario.
+2  A: 

What you're describing is a Barrier in concurrency terms and is quite popular in many types of applications.

Whether or not it is suitable for your application is difficult to say without more details.

Ben S
There is a definite benefit and a definite speed increase, at least with the basic tests that I have done already. However, I am curious if it will be enough to justify creating a robust solution that uses threads both on Win32 and on Linux.
+1  A: 

If you have multi-core processors then you might be able to get threads to run at the same time and get speedup via parallelism.

However, I believe the main use for threads is to allow multiple logical chains of actions to be more easily written, especially if they involve waiting for external events, like user input or I/O completion.

Mike Dunlavey
+4  A: 

Unless your worker threads are some set of pathological cases that get no parallelization benefits, they'll probably complete faster in parallel than serially. So the question of whether it's worth it becomes one of how much time that saves and how much that's worth to you in context. If there's an antsy user waiting for a mouse click to do something, then saving 5 out of 10 seconds is a meaningful benefit. Same savings in a cron job, not so much.

If you think there's a decent chance that it'll do something useful for you, build a quick test version and profile it.

chaos