I am currently testing out pthreads for usage in order to speed up other code I have. Essentially what my code currently has is a couple of for loops that end up doing matrix calculations. I tried spawning threads that will do those calculations simultaneously, and it ended up cutting the runtime in half.
My question is that, is there a way to initially spawn these pthreads, have them run the function, and use them again later to run the same function without them closing? Or is it necessary to close them after performing the task?
I am using Windows by the way, using a pthreads library.
You could create a
queue
of tasks, that your threads will sample for new tasks and dequeue if any are present (and preferably some cancellation flag to gracefully stop them).You'd need to make the addition of tasks thread-safe by e.g using a
pthread_mutex
.You could also use a conditional variable (
pthread_cond_t
) to signal when are there more tasks to cunsumeThis way you could spawn as many threads as you wish in the beginning, and just add tasks when needed.
As a side note you should probably prefer the less cumbersome
thread
ing of the standard library.