I am writing a program for an assignment,which implements matrix multiplication between two given arrays,using threads.
I must give the number of threads i want to use as a command line argument and if the number of them is smaller than the number of lines of the first array,reuse some the same threads until the whole job is done.
I have managed to make it work but only using a thread for each row of the array.
For example if a have a 5×5 multiplication and use less than 5 threads i take a segmentation fault,which makes sense.
My question is:How can i reuse a thread after this thread has finished its job?
In the previous example i mean that if i want to use 2 threads for a 5×5 my program should work like this:
thread 1->line 1
thread 2->line 2
thread 2->line 3
thread 1->line 4
etc.
You have several possibilities, but the main idea is that you must monitor threads to detect when they have finished their job, and have a way to know if there’s still something left to do.
The first way that comes to mind is to have a dedicated thread to track all the running threads, and recycle them once they’re finished. But in order to do that, you need a mechanism to let threads synchronize, which may be implemented using semaphores or mutex, or messages, but it could be cumbersome to code just for that purpose if you don’t have a need for it already.
The second way is simply to ask them to recycle themselves, since they know when they are done. In many other languages, there’s a mechanism called continuations, which let you exactly that, but since we’re dealing with C, we need to do that by hand. Luckily, here the continuation is actually just one single task.
So the mechanism to call the continuation is actually just a function, which will first run the task to be executed by the thread, and then either:
Obviously, the first option would be easier, and in your case, you already know at setup time what has to be done, so your setup function could fill up a list of tasks and then launch as many threads as you want, and let them do the recycling themselves.
Here’s a simple skeleton you could start with:
This is a basic outline of what you could do, and should get you started.
There are several questions on SO dealing with C linked lists. The one here must be synchronized, should not block, and return
NULLwhen empty.