0

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]);
}
Community
  • 1
  • 1
Conceited Code
  • 4,517
  • 3
  • 29
  • 32
  • 3
    http://stackoverflow.com/questions/481970/how-many-threads-is-too-many/481979#481979 - despite the fact it's a python rather than C tag on that question, it's probably a dupe since the underlying language used is irrelevant. – paxdiablo May 16 '11 at 22:16

4 Answers4

1

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?

Aater Suleman
  • 2,286
  • 18
  • 11
  • A good rule of thumb is, use one software thread per hardware thread. Given Intel HyperThreading (HT), there can be 2 threads per core; otherwise expect 1 thread per core. Use a tool like CPU-Z to determine no. of hardware threads per core. – Engineer Feb 19 '15 at 09:57
0

If you aren't blocking the threads to do disk or network I/O then the most efficient number of threads in the pool is one per-processor (assuming all the work is being done on the thread pool threads). The rest of the work items should be queued up and pulled off as they are completed.

Laurion Burchall
  • 2,843
  • 16
  • 12
0

If all you want is to parallelize a large for loop with independent iterations, maybe you can use OpenMP.

int i;
#pragma omp parallel for
for(i = 0; i < 10000000; i++) {
    // the body of your loop
}

You can select different schedule options and other parameters to tune behavior, including the number of threads if you need; by default, OpenMP will create as many threads as there are hardware contexts available, and divide the work in equal proportion between all threads.

Alexey Kukanov
  • 12,479
  • 2
  • 36
  • 55
0

I have a thread pool of a certain size, and I want to reuse the threads.

You probably want to look on producer-consumers pattern. In short: you first create your threads, which call some func, which returns things to process if there are any, or just blocks thread if there are none. It can be implemented in Windows by CreateUserACP (or QueueUserACP) or {Post,Get}QueuedComplentionStatus API's. In UNIXies - with pthread_cond_* or pthread_workqueue_* calls.

arrowd
  • 33,231
  • 8
  • 79
  • 110