Possible Duplicate:
How many threads is too many?
I have a large for loop, in which I want each item to be passed to a function on a thread. I have a thread pool of a certain size, and I want to reuse the threads. What is the most efficient way to do this?
What would be the most efficient size for the thread pool? Or at least what is the best way to figure it out?
pthread_t thread_id[10];
int i;
for(i = 0; i < 10000000; i++) {
pthread_create(thread_id[?], NULL, threadFunc, params[i]);
}
Too few threads means wasted performance opportunity and too many threads wastes resources consumed by each thread and can reduce performance due to contention among threads. I would use measurements and see what number works the best. Just curious, is there are reason why you want each iteration to be a new thread?