I have list with 1000-10000 jobs. It’s stored in simple char array. I need to finish work on the list as fast as i can.
I want to run 10 threads at any time. How i have to run new thread immediately (or almost immediately) after previous thread is done?
#define THREADS_LIMIT 10
const char * const jobs[]= { "data1", .... }
...
for (i = 0; i < THREADS_LIMIT; ++i)
{
// run first 10 threads
}
How can i start thread after previous is done ? I can run 10 threads, then wait until ALL of the is done, and then run next 10. But is there more efficient way ?
Don’t stop any threads. Simply have each thread move on to the next job instead of destroying it and creating a new one; they terminate when no more work remains. Now your main routine just starts THREAD_LIMIT threads, then
pthread_joins them all.