C++11 线程池

在这里插入图片描述

cpp

#include<thread>
#include<mutex>
#include<vector>
#include<condition_variable>
#include<boost/circular_buffer.hpp>
#include<functional>
#include<atomic>
#include<cassert>


class ThreadPool
{
public:
	explicit ThreadPool(size_t n) :q_{ n }, running_{ false }{}
	~ThreadPool()
	{
		Stop();
	}
	void Start(size_t n)
	{
		if (running_)return;
		running_ = true;
		threads_.reserve(n);
		while (n--)
		{
			threads_.emplace_back(&ThreadPool::Worker, this);
		}
	}
	template<typename Fun>
	void submit(Fun f)
	{
		std::unique_lock<std::mutex> lk{ mtx_ };
		not_full_.wait(lk, [this] {return !running_ || !q_.full(); });
		assert(!running_||!q_.full());
		if (!running_) return;
		q_.push_back(std::move(f));
		not_empty_.notify_one();
	}
	void Stop()
	{
		if (!running_)return;;
		running_ = false;
		not_full_.notify_all();
		not_empty_.notify_all();
		for (auto& th : threads_)
		{
			if (th.joinable())th.join();
		}
	}

private:
	void Worker()
	{
		while (true)
		{
			task t;
			{
				std::unique_lock<std::mutex> lk{ mtx_ };
				not_empty_.wait(lk, [this] {return !running_ || !q_.empty(); });
				assert(!running_ || !q_.empty());
				if (!running_)return;
				t =  std::move(q_.front()) ;
				q_.pop_front();
				not_full_.notify_one();
			}
			t();
		}
	}
private:
	using task = std::function<void()>;
	std::vector<std::thread>threads_;
	boost::circular_buffer<task>q_;
	std::mutex mtx_;
	std::condition_variable not_full_;
	std::condition_variable not_empty_;
	std::atomic<bool> running_;
};

#include<iostream>
int main()
{
	ThreadPool pool{ 8 };
	std::mutex mtx;
	pool.Start(4);
	for (int i = 0; i < 100; ++i)
	{
		pool.submit(
			[i]{
				std::cout << i << " " << std::this_thread::get_id() << std::endl;
			});
	}
	return 0;
}

可传参带返回值

#include <vector>
#include <thread>
#include <boost/circular_buffer.hpp>
#include <functional>
#include <mutex>
#include <condition_variable>
#include <future>
#include <atomic>

class task {
public:
	task() = default;

	template <class Fun>
	explicit task(Fun&& f) : ptr_{ new wrapper{ std::move(f) } } {}

	void operator()() { ptr_->call(); }

private:
	class wrapper_base {
	public:
		virtual void call() = 0;
		virtual ~wrapper_base() = default;
	};

	template <class Fun>
	class wrapper : public wrapper_base {
	public:
		explicit wrapper(Fun&& f) : f_{ std::move(f) } {}
		virtual void call() override { f_(); }

	private:
		Fun f_;
	};

	std::unique_ptr<wrapper_base> ptr_;
};

class thread_pool {
public:
	explicit thread_pool(size_t n) : q_{ n }, running_{ false } {}

	~thread_pool() { stop(); }

	void start(size_t n) {

		if (running_) return;

		running_ = true;
		threads_.reserve(n);
		while (n--) {
			threads_.emplace_back(&thread_pool::worker, this);
		}
	}

	void stop() {

		if (!running_) return;
		running_ = false;
		not_full_.notify_all();
		not_empty_.notify_all();

		for (auto& t : threads_) {
			if (t.joinable()) t.join();
		}
	}

	template <class Fun,typename ...Args>
	auto submit(Fun&& f,Args&&...args)->std::future<decltype(f(args...))> {

		using RetType = decltype(f(args...));
		auto pt = std::make_unique<std::packaged_task<RetType()>>( std::bind(std::forward<Fun>(f), std::forward<Args>(args)...) );
		auto ret = pt->get_future();
		task t{ std::move(*pt) };

		{
			std::unique_lock lk{ m_ };
			not_full_.wait(lk, [this] { return !running_ || !q_.full(); });

			assert(!running_ || !q_.full());

			if (!running_) return {};

			q_.push_back(std::move(t));
			not_empty_.notify_one();
		}

		return ret;
	}

private:
	void worker() {

		while (true) {

			task t;
			{
				std::unique_lock lk{ m_ };
				not_empty_.wait(lk, [this] { return !running_ || !q_.empty(); });

				assert(!running_ || !q_.empty());

				if (!running_) return;

				t = std::move(q_.front());
				q_.pop_front();
				not_full_.notify_one();
			}
			t();
		}
	}

	std::vector<std::thread> threads_;
	boost::circular_buffer<task> q_;
	std::mutex m_;
	std::condition_variable not_full_;
	std::condition_variable not_empty_;
	std::atomic<bool> running_;
};

#include <iostream>
int func(int a)
{
	std::cout << "id:" << std::this_thread::get_id() << std::endl;
	return a;
}

int main() {

	thread_pool pool{ 10 };
	pool.start(3);
	std::vector<std::future<int>> rets;
	for (int i = 0; i < 100; i++) {
		auto ret = pool.submit(func, i);
		rets.push_back(std::move(ret));
	}
	for (auto& ret : rets) {
		std::cout << ret.get() << " ";
	}
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吃米饭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值