也来写个线程池

也来写个线程池

 

#include <iostream>
#include <vector>
#include <memory>
#include <thread>
#include <mutex>
#include <queue>
#include <condition_variable>
#include <functional>
#include <chrono>

template <class T>
class Queue {
public:
    explicit Queue(uint32_t size = 2000) : queueSize_(size) {}
    void Push(T& data)
    {
        std::unique_lock<std::mutex> lk(mtx_);
        if (IsFull()) {
            notFullCondVar_.wait(lk, [&] { return !IsEmpty() || quit_; });
        }

        if (!IsFull()) {
            queue_.push(data);
            notEmptyCondVar_.notify_all();
        }
    }
    bool Pop(T& data)
    {
        std::unique_lock<std::mutex> lk(mtx_);
        if (IsEmpty()) {
            notEmptyCondVar_.wait(lk, [&] { return !IsFull() || quit_; });
        }

        if (!IsEmpty()) {
            data = queue_.front();
            queue_.pop();
            notFullCondVar_.notify_all();
            return true;
        }

        return false;
    }
    void Quit() { quit_ = true; }
    bool IsFull() { return queue_.size() == queueSize_; }
    bool IsEmpty() { return queue_.empty(); }
private:
    uint32_t queueSize_;
    std::queue<T> queue_;
    std::mutex mtx_;
    std::condition_variable notFullCondVar_;
    std::condition_variable notEmptyCondVar_;
    bool quit_ = false;
};

struct Task {
    std::function<void(void *)> process_;
    void *data;
};

void Process(void *data)
{
    std::cout << "process task " << (uint32_t)(uintptr_t)data << std::endl;
}

class ThreadPool {
public:
    explicit ThreadPool(uint32_t num = 4) : threadNum_(num) {}

    void Start()
    {
        working_ = true;
        for (auto i = 0; i < threadNum_; i++) {
            pool_.push_back(std::thread(std::bind(&ThreadPool::Work, this)));
        }
    }

    void Stop()
    {
        que_.Quit();
        working_ = false;
        for (auto i = 0; i < threadNum_; i++) {
            if (pool_[i].joinable()) {
                pool_[i].join();
            }
        }
    }

    void Work()
    {
        std::shared_ptr<Task> task;
        while (working_) {
            auto ret = que_.Pop(task);
            if (ret == false) {
                std::this_thread::sleep_for(std::chrono::milliseconds(10));
                continue;
            }
            task->process_(task->data);
        }
    }

    void Push(std::shared_ptr<Task> task)
    {
        que_.Push(task);
    }


private:
    uint32_t threadNum_;
    std::vector<std::thread> pool_;
    Queue<std::shared_ptr<Task>> que_;
    bool working_;
};

int main() {
    ThreadPool tp(4);
    tp.Start();
    uint32_t i = 0;
    while (1) {
        std::shared_ptr<Task> task = std::make_shared<Task>();
        task->process_ = Process;
        task->data = (void *)(uintptr_t)i;
        i++;
        tp.Push(task);
        if (i == 10) {
            break;
        }
    }
    std::this_thread::sleep_for(std::chrono::seconds(10));
    tp.Stop();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值