#include <iostream>
#include <thread>
#include <deque>
#include <functional>
#include <future>
#include <mutex>
#include <condition_variable>
#include <vector>
#include <memory>

class ZYLThreadPool {
public:
    ZYLThreadPool(size_t maxThreads = 200): _maxThreads(maxThreads), _stop(false) {
        worker_threads.reserve(_maxThreads);
        for(size_t i = 0; i < _maxThreads; ++i) {
            worker_threads.emplace_back([this] {
                for(;;) {
                    std::function<void()> task;
                    {
                        std::unique_lock<std::mutex> lock(this->_mutex);
                        this->_condVar.wait(lock, [this] {
                            return this->_stop || !this->_taskQueue.empty();
                        });
                        if(this->_stop && this->_taskQueue.empty()) return;
                        task = std::move(this->_taskQueue.front());
                        this->_taskQueue.pop_front();
                    }
                    task();
                }
            });
        }
    }

    template<typename F, typename… Args>
    std::future<typename std::result_of<F(Args…)>::type> submit(F&& f, Args&&… args) {
        typedef typename std::result_of<F(Args…)>::type return_type;

        auto task = std::make_shared<std::packaged_task<return_type()>>(std::bind(std::forward<F>(f), std::forward<Args>(args)…));

        std::future<return_type> result = task->get_future();
        {
            std::unique_lock<std::mutex> lock(_mutex);
            if (_stop) {
                throw std::runtime_error(“Submit on stopped ZYLThreadPool”);
            }
            _taskQueue.emplace_back([task] { (*task)(); });
        }
        _condVar.notify_one();
        return result;
    }

    void cancel() {
        std::unique_lock<std::mutex> lock(_mutex);
        if (!_stop) {
            _stop = true;
            _condVar.notify_all();
        }
        for(std::thread &worker: worker_threads) worker.join();
    }

    size_t getNumTasks() {
        std::unique_lock<std::mutex> lock(_mutex);
        return _taskQueue.size();
    }

    ~ZYLThreadPool() {
        cancel();
    }

private:
    std::vector<std::thread> worker_threads;
    std::deque<std::function<void()>> _taskQueue;
    size_t _maxThreads;
    std::mutex _mutex;
    std::condition_variable _condVar;
    bool _stop;
};


class Test {
public:
    Test(double devid): _devid(devid), _running(true) {
    }

    void start(ZYLThreadPool &threadPool) {
        _future = threadPool.submit(&Test::_task, this);
    }

    void stop() {
        {
            std::unique_lock<std::mutex> lock(_mutex);
            _running = false;
            if (_future.valid()) {
                _future.get();
                lock.unlock();
                _cleanupResources();
            }
        }
    }

private:
    std::mutex _mutex;
    double _devid;
    std::deque<double> _dequeue_packets;
    std::future<void> _future;
    bool _running;

    void _cleanupResources() {
        std::unique_lock<std::mutex> lock(_mutex);
        _dequeue_packets.clear();
    }

    void _task() {
        std::unique_lock<std::mutex> lock(_mutex);
        while(_running && !_dequeue_packets.empty()) {
            double packet = _dequeue_packets.front();
            _dequeue_packets.pop_front();
            lock.unlock();

            // 对 packet 进行处理…

            lock.lock();
        }
    }
};

int main() {
    ZYLThreadPool zylThreadPool(200);
    std::shared_ptr<Test> test = std::make_shared<Test>(1.0);
    
    test->start(zylThreadPool);
    std::this_thread::sleep_for(std::chrono::seconds(1));
    test->stop();

    return 0;
}

代码在linux环境下有报错，error: extended character … is not valid in an identifier
   34 |     template<typename F, typename… Args>
   请修复