Sobes.tech
Junior — Middle

What is the difference between using a thread pool and creating new threads on demand?

sobes.tech AI

Answer from AI

The main difference between using a thread pool and creating new threads as needed:

  • Creating new threads as needed — a new thread is created each time, which is costly in terms of time and resources, especially with a large number of tasks.
  • Thread pool — a fixed number of threads are created in advance and reused for executing tasks. This reduces overhead for creating and destroying threads, improves performance, and manageability.

An example in C++ using a thread pool (simplified):

#include <thread>
#include <vector>
#include <queue>
#include <functional>
#include <condition_variable>
#include <mutex>
#include <atomic>

class ThreadPool {
    std::vector<std::thread> workers;
    std::queue<std::function<void()>> tasks;
    std::mutex queue_mutex;
    std::condition_variable condition;
    std::atomic<bool> stop;

public:
    ThreadPool(size_t threads) : stop(false) {
        for(size_t i = 0; i < threads; ++i) {
            workers.emplace_back([this] {
                while(true) {
                    std::function<void()> task;
                    {
                        std::unique_lock<std::mutex> lock(this->queue_mutex);
                        this->condition.wait(lock, [this]{ return this->stop || !this->tasks.empty(); });
                        if(this->stop && this->tasks.empty())
                            return;
                        task = std::move(this->tasks.front());
                        this->tasks.pop();
                    }
                    task();
                }
            });
        }
    }

    void enqueue(std::function<void()> task) {
        {
            std::unique_lock<std::mutex> lock(queue_mutex);
            tasks.push(std::move(task));
        }
        condition.notify_one();
    }

    ~ThreadPool() {
        stop = true;
        condition.notify_all();
        for(std::thread &worker : workers)
            worker.join();
    }
};

Thus, a thread pool optimizes resource usage and enhances the efficiency of multithreaded processing.