C++11线程池

 使用 condition_variable::wait(unique_lock<mutex>&lck, Predicate pred) 时,必须保证条件变量通过notify唤醒的同时,wait 的第二个参数 Predicate 返回 true 了才可以往下走。必须两个条件同时满足,如果notify的时候Predicate返回时false一样的唤不醒线程。

#include <iostream>
#include <future>
#include <chrono>
#include <string>
#include<functional>
#include <queue>

using Task = std::function<void()>;
class ThreaPool
{
public:
    void start() 
    {
        running_ = true;
    }

    bool is_running()
    {
        return running_;
    }

    void stop() 
    {
        std::cout << "stop..." << std::endl;
        running_ = false;
        std::unique_lock<std::mutex> lk(qmutext_);
        tasks_.push([]() { std::cout << "hello pool end " << std::endl;  });
        cond_.notify_all();
    }

    void push_task(Task task) 
    {
        if (!running_)
        {
            return;
        }
        

std::unique_lock<std::mutex> lk(qmutext_);
        tasks_.push(task);
        cond_.notify_one();
    }

    ThreaPool(int count)
    {
        for (int i = 0; i < count; i++)
        {
            pool_.emplace_back([this]() 
            {
                while (true)
                {
                    Task task;
                    {
                        std::unique_lock<std::mutex> lk(qmutext_);
                        std::cout << "wait 1" << std::endl;
                        cond_.wait(lk, [this] {return !running_ || !tasks_.empty(); });
                        std::cout << "wait 2" << std::endl;
                        if (!tasks_.empty())
                        

{
                            task = std::move(tasks_.front());
                            tasks_.pop();
                        }
                    }

                    if (task)
                    {
                        task();
                    }

                    if (!this->is_running())
                    {
                        std::cout << "stoped 1" << std::endl;
                        std::unique_lock<std::mutex> lk(qmutext_);
                        if (tasks_.empty()) {
                            std::cout << "stoped 2" << std::endl;
                            return;
                        }
                    }
                }
            });
        }
    }

~ThreaPool() 
	{
		for (std::thread& worker : pool_) {
			worker.join();
		}
	};

private:
	std::vector<std::thread> pool_;
	std::mutex qmutext_;
	std::condition_variable cond_;
	std::queue< Task > tasks_;
	std::atomic_bool running_{ true };
};

int main()
{
	ThreaPool pool(5);

	std::this_thread::sleep_for(std::chrono::seconds(1));
	pool.push_task([]() { std::cout << "hello pool 1 \n" << std::endl;  });
	pool.push_task([]() { std::cout << "hello pool 2 \n" << std::endl;  });
	pool.push_task([]() { std::cout << "hello pool 3 \n" << std::endl;  });
	pool.push_task([]() { std::cout << "hello pool 4 \n" << std::endl;  });
	pool.push_task([]() { std::cout << "hello pool 5 \n" << std::endl;  });
    pool.push_task([]() { std::cout << "hello pool 6 \n" << std::endl;  });
	pool.push_task([]() { std::cout << "hello pool 7 \n" << std::endl;  });
	pool.push_task([]() { std::cout << "hello pool 8 \n" << std::endl;  });
	pool.push_task([]() { std::cout << "hello pool 9 \n" << std::endl;  });

	std::this_thread::sleep_for(std::chrono::seconds(1));
	pool.stop();
	return 0;
}
                    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

tangcpp

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

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

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

打赏作者

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

抵扣说明:

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

余额充值