Thread Pools — In a Nutshell

Alperen Bayramoğlu
2 min readJul 23, 2023

--

In computer programming, a thread pool is a software design pattern for achieving concurrency of execution in a computer program. — Wikipedia

Photo by Steve Johnson on Unsplash

If we want to execute our tasks in a multithreaded environment, like running every task in its own thread, we should create a thread for every task. (This task can be an API request, a new WebSocket connection, etc.)

But the creation and running of threads are not cheap and may induce the problem of waiting for multiple threads. This situation deducts performance.

Thread pools come as a solution to this. Thread pools contain a certain number of worker threads that run forever. (e.g. until the stop/cancellation token)

In most of the implementations, queues are used to give tasks to threads in a thread pool. Multiple threads listen for the queue to fetch tasks (so the queue should be thread-safe). A Thread takes a task, does it, and waits for the next task. (e.g. Single Producer — Multiple Consumer problem)

Thread Pool — Image by Author

Practical Example with the POSIX Threads in C

Don’t forget to link with the POSIX threads library. ( -lpthread )

--

--

No responses yet