Multithreading – pthreads – how to parallelize jobs
I need to parallelize a simple password cracking program to use it on N processor systems My idea is to create n threads and provide them with more and more work after completion
What is the best way to know when a thread completes? Mutually exclusive? Is it expensive to keep checking this mutex while other threads are running?
Solution
You can have a simple queue structure – use any data structure you like – and then use mutexes when adding / deleting items
If your threads grab what they need to do with a large enough "block", there is little contention on the mutex, so the overhead is very small
For example, if each thread grabs about one second of work at a time and works independently for one second, there will be few operations on the mutex
Threads can exit when there is no more work; The main thread can then use pthread_ Join wait