Reputation: 391
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.
Upvotes: 0
Views: 240
Reputation: 6577
Why not to use full-fledged parallel-processing libraries or language extensions like openmp, cilk, tbb. All these are quite portable now, having support from main OSes and compilers. Microsoft also has ppl which is TBB's twin.
So, you don't have to invent the wheel but let the libraries to take care of threads and the load balance; and prevent you from traps like this: Why is OpenMP outperforming threads?
For example, the vector of tasks can be run in parallel by default number of threads as simple as (in cilk):
cilk_for(int i = 0; i < tasks.size(); i++)
task[i].some_function();
You can also change the number of threads if needed.
Upvotes: 0
Reputation: 2984
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 cunsume
This 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.
Upvotes: 2