前面已经说到了同步队列的实现,下面来看线程池的实现。
#ifndef INCLUDE_THREADPOOL__
#define INCLUDE_THREADPOOL__
#include <list>
#include <thread>
#include <memory>
#include <atomic>
#include "SyncQueue.hpp"
namespace MyThreadPool {
class ThreadPool {
using Task = std::function<void()>;
public:
ThreadPool(int max_size = std::thread::hardware_concurrency())
: sync_queue_{
max_size} {
running_ = true;
for (int i = 0; i < max_size; ++i) {
threads_.emplace_back(
std::make_shared<std::thread