views:

868

answers:

3

I need to parallelize a simple password cracker, for using it on a n-processor system. My idea is to create n threads and to feed them more and more job as they finish.

What is the best way to know when a thread has finished? A mutex? Isn't expensive checking this mutex constantly while other threads are running?

+4  A: 

You can have a simple queue structure - use any data structure you like - and then just use a mutex when you add/remove items from it.

Provided your threads grab the work they need to do in big enough "chunks", then there will be very little contention on the mutex, so very little overhead.

For example, if each thread was to grab approximately 1 second of work at a time and work independently for 1 second, then there would be very few operations on the mutex.

The threads could exit when they had no more work; the main thread could then wait using pthread_join.

MarkR
ok, i almost got it... one last thing: what thread should the main thread join? even if all the sub-threads have roughly the same amount of work you can never tell which will finish first...
zakk
Just join all of them, in any order; joining a thread which has already finished will return immediately.
MarkR
+1  A: 

Use message queues between the threads :-

Master -> Process (saying go with this). Process -> Master (saying I'm done - give me more, or, I've found the result!)

Using this, the thread only closes down when the system does - otherwise it's either processing data or waiting on a message queue.

This way, the MCP (I've always wanted to say that!) simply processes messages and hands jobs out to threads that are waiting for more work.

This may be more efficient that creating and destroying threads all the time.

James Fisher
+1  A: 

Normally you use a "condition variable" for this kind of thing where you want to wait for an asynchronous job to finish.

Condition variables are basically mutex-protected, simple signals. Pthread has condition variables (see e.g. the pthread_cond_create(...) function).

antti.huima