Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should theadpool create and destroy threads dynamically? #70

Open
foreverpersist opened this issue May 3, 2018 · 3 comments
Open

Should theadpool create and destroy threads dynamically? #70

foreverpersist opened this issue May 3, 2018 · 3 comments

Comments

@foreverpersist
Copy link

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
Copy link

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
Copy link
Author

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
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants