/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#pragma once
#ifndef THREADPOOL_HPP
#define THREADPOOL_HPP

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

class ThreadPool {
  public:
    ThreadPool();
    explicit ThreadPool(uint32_t MaxThreads);
    void addJob(const std::function<void()> &Job);
    int numberOfThreads();
    void Stop();
    bool waitEnd();
    ~ThreadPool();

  private:
    void spin();

    bool Terminate = false;
    bool Terminated = false;
    std::atomic_int WorkingThreads = 0;
    std::mutex QueueMutex;
    std::condition_variable MutexCondition;
    std::condition_variable JobCondition;
    std::vector<std::thread> Threads;
    std::queue<std::function<void()>> Jobs;
};

#endif /* THREADPOOL_HPP */
