#ifndef THREAD_POOL_H
#define THREAD_POOL_H
#include <thread>
#include <mutex>
#include <vector>
#include <queue>
#include <functional>
#include <condition_variable>
#include <shared_mutex>

namespace zsy
{
    class ThreadPool
    {
        std::vector<std::jthread> threads; // 工作线程
        std::queue<std::function<void()> > tasks; // 任务队列
        mutable std::recursive_mutex queueMutex; // 保护任务队列的互斥锁
        std::condition_variable_any taskCondition; // 任务通知条件变量
        std::string name;
        // 0-->不可用，1-->已启动，2-->已关闭
        uint8_t status; // 线程池停止标志
        uint16_t threadCount; // 线程池停止标志
        std::shared_mutex status_mtx;

        void addTask(const std::function<void()> &task);

        void createThread();

    public:
        explicit ThreadPool(std::string name, uint16_t threadCount = std::thread::hardware_concurrency() * 2);

        void submit(const std::function<void()> &task);

        ~ThreadPool();

        ThreadPool(const ThreadPool &) = delete;

        ThreadPool &operator=(const ThreadPool &) = delete;

        ThreadPool(ThreadPool &&) = delete;

        ThreadPool &operator=(ThreadPool &&) = delete;
    };
} // zsy

#endif //THREAD_POOL_H
