Pithikos / C-Thread-Pool

A minimal but powerful thread pool in ANSI C
MIT License
2.04k stars 602 forks source link

Should theadpool create and destroy threads dynamically? #70

Open foreverpersist opened 6 years ago

foreverpersist commented 6 years ago

Current threadpool in this project creates threads statically when it is initialized. And number of threads is fixed during its lifecycle.

Why not create and destroy threads dynamically?

Create:

  • case 1: number of current threads is less than number of core threads.
  • case 2: number of current threads is less than number of max threads, and no idle threads exist now.

Destroy:

  • thread has waited for keep_alive_time time interval without jobs fetched.
  • core threads can ignore keep_alive_time and keep alive if allowed.
JayWalker512 commented 5 years ago

I think the whole point of a thread pool (statically created threads at init time) is to avoid the overhead of creating/destroying threads repeatedly during run time. The penalty for having idle threads in the background is basically none, so it's not worth the effort to destroy them.

foreverpersist commented 5 years ago

I think the whole point of a thread pool (statically created threads at init time) is to avoid the overhead of creating/destroying threads repeatedly during run time. The penalty for having idle threads in the background is basically none, so it's not worth the effort to destroy them.

Sure, creating/destroying threads may bring some overhead, but is it significant performance overhead?It may be difficult to estimate each workload when creating threads statically. Too many static threads may waste resources, and too few threads can not deal with tasks well. That is why I think it is meaningful to create/destroy threads dynamically.

marwankallal commented 5 years ago

Late to the party but my two cents

Other than the fact that dynamically allocating the threads would make this not a thread pool: embedded applications and other applications where heap space and cycles are severely limited exist, and using malloc in many codebases is frowned upon. The main point of a threadpool is that you can have a sort of fake dynamic allocation depending on how many jobs come through, because malloc and free are very expensive operations compared to leaving a thread idle.