rusty_pool icon indicating copy to clipboard operation
rusty_pool copied to clipboard

I am looking for a thread pool that can expand its capacity dynamically based on demand.

Open dochy-ksti opened this issue 7 months ago • 1 comments

I am looking for a thread pool that can expand its capacity dynamically based on demand. My current understanding of your library's implementation is that when execute is called, it will create a new thread until the core_size limit is met, even if idle threads are already present in the pool.

To achieve my desired functionality, it would be highly beneficial to have a method that assigns tasks to an existing idle thread rather than spawning a new one. I kindly request you consider incorporating this feature. Your consideration is greatly appreciated.

dochy-ksti avatar Jun 06 '25 04:06 dochy-ksti

The pool always uses an idle thread if available. core_size is the number of threads that live for as long as the pool (unaffected by keep_alive time and kept idle until the pool is destroyed). But those threads need to be spawned at some point, ThreadPool::new does not spawn any threads, so if your core_size is 4, the first 4 execute() calls will spawn a core thread. This is simply a mechanism to fill the pool initially, all consecutive calls will use an idle thread if there is one. If you want to spawn the core threads immediately when creating the pool instead of on demand, you can use the start_core_threads function. Alternatively, you can also set core_size to 0, but that will of course cause a new thread to be spawned every time the pool has been idle for longer than the keep_alive time, as all threads will have terminated.

robinfriedli avatar Aug 07 '25 14:08 robinfriedli