简单线程池实现

简单线程池实现

thpoll.h

#pragma once

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

namespace thPool {

    class fixed_thread_pool {
    public:
        explicit fixed_thread_pool(size_t thread_count)
            : data_(std::make_shared<data>()) {
            for (size_t i = 0; i < thread_count; ++i) {
                std::thread([data = data_] {
                    std::unique_lock<std::mutex> lk(data->mtx_);

                    for (;;) {
                        if (!data->tasks_.empty()) {
                            auto current = std::move(data->tasks_.front());
                            data->tasks_.pop();
                            lk.unlock();
                            current();
                            lk.lock();
                        }
                        else if (data->is_shutdown_) {
                            break;
                        }
                        else {
                            data->cond_.wait(lk);
                        }
                    }
                    }).detach();
            }
        }

        fixed_thread_pool() = default;
        fixed_thread_pool(fixed_thread_pool&&) = default;

        ~fixed_thread_pool() {
            if ((bool)data_) {
                {
                    std::lock_guard<std::mutex> lk(data_->mtx_);
                    data_->is_shutdown_ = true;
                }
                data_->cond_.notify_all();
            }
        }

        template <class F>
        void execute(F&& task) {
            {
                std::lock_guard<std::mutex> lk(data_->mtx_);
                data_->tasks_.emplace(std::forward<F>(task));
            }
            data_->cond_.notify_one();
        }

    private:
        struct data {
            std::mutex mtx_;
            std::condition_variable cond_;
            bool is_shutdown_ = false;
            std::queue<std::function<void()>> tasks_;
        };
        std::shared_ptr<data> data_;
    };

    //包装一下
    class Thread : public fixed_thread_pool {

    public:
        Thread(size_t _thread_count) : fixed_thread_pool(_thread_count)
        {
            std::cout << "thread_count:" << _thread_count << std::endl;
            fflush(stdout);
        }
        ~Thread() {};

    public:
        static Thread& Instance()
        {
            //设定10个初始线程
            static Thread instance(10);
            return instance;
        }
    };
};

main.c

#include <iostream>
#include "thpoll.h"
#include <windows.h>

int main()
{
    
    thPool::Thread::Instance().execute([&]()-> void {
        while (1)
        {
            printf("4\n");
            Sleep(1000);
        }
        });
    
    thPool::Thread::Instance().execute([&]()-> void {
        while (1)
        {
            printf("5\n");
            Sleep(1000);
        }
        });
    
    
    while (1)
    {
        std::cout << "11111 .. " << std::endl;
        Sleep(1000);
    }
}

输出

thread_count:10
11111 ..
4
5
4
11111 ..
5
4
11111 ..
5

参考链接

posted @ 2021-04-14 10:21  做个奇怪的人  阅读(52)  评论(0编辑  收藏  举报