也可参考:  https://www.cnblogs.com/ailumiyana/p/10016965.html  ***
https://blog.csdn.net/jlusuoya/article/details/74505558  ***
 https://www.cnblogs.com/magicsoar/p/3788180.html  *****
 
//nocopyable类,不可拷贝基类继承它,派生类不可拷贝
//nocopyable.h
#ifndef NOCOPYABLE_H
#define NOCOPYABLE_H
 
namespace fivestar
{
  class nocopyable
  {
  private:
      nocopyable(const nocopyable& x) = delete;
      nocopyable& operator=(const nocopyable&x) = delete;
  public:
      nocopyable() = default;
      ~nocopyable() = default;
  };
 
}
 
#endif // NOCOPYABLE_H
 
//ThreadPool.h
#ifndef THREADPOOL_H
#define THREADPOOL_H
 
#include <thread>
#include <mutex>
#include <functional>
#include <string>
#include <condition_variable>
#include <deque>
#include <vector>
#include <memory>
 
#include "nocopyable.h"
namespace fivestar
{
  class ThreadPool:public nocopyable
  {
  public:
      typedef std::function<void()> Task;
 
      explicit ThreadPool(const std::string &name = std::string());
      ~ThreadPool();
 
 
      void start(int numThreads);//设置线程数,创建numThreads个线程
      void stop();//线程池结束
      void run(const Task& f);//任务f在线程池中运行
      void setMaxQueueSize(int maxSize) { _maxQueueSize = maxSize; }//设置任务队列可存放最大任务数
 
private:
      bool isFull();//任务队列是否已满
      void runInThread();//线程池中每个thread执行的function
      Task take();//从任务队列中取出一个任务
 
      std::mutex _mutex;
      std::condition_variable _notEmpty;
      std::condition_variable _notFull;
      std::string _name;
      std::vector<std::thread> _threads;
      std::deque<Task> _queue;
      size_t _maxQueueSize;
      bool _running;
  };
}
 
#endif // THREADPOOL_H
 
 

注意:

1 .为线程池添加任务之前一定要调用setMaxQueueSize,设置任务队列可存放的最大任务数,否则线程池退化为单线程

2 若不调用start创建线程,则线程池退化为单线程

测试代码

#include <iostream>
#include "ThreadPool.h"
 
using namespace std;
 
void Test(int i)
{
    printf("I love you %d time\n",i);
}
 
int main()
{
    fivestar::ThreadPool threadPool;
    threadPool.setMaxQueueSize(10);
    threadPool.start(2);
 
    for(int i = 0;i < 10;++i)
    {
        auto task = bind(Test,i);
        threadPool.run(task);
    }
 
    getchar();
    return 0;
}
 
 
 
 

c++11 线程池的更多相关文章

  1. 托管C++线程锁实现 c++11线程池

    托管C++线程锁实现   最近由于工作需要,开始写托管C++,由于C++11中的mutex,和future等类,托管C++不让调用(报错),所以自己实现了托管C++的线程锁. 该类可确保当一个线程位于 ...

  2. 简单的C++11线程池实现

    线程池的C++11简单实现,源代码来自Github上作者progschj,地址为:A simple C++11 Thread Pool implementation,具体博客可以参见Jakob's D ...

  3. c++11 线程池学习笔记 (一) 任务队列

    学习内容来自一下地址 http://www.cnblogs.com/qicosmos/p/4772486.html github https://github.com/qicosmos/cosmos ...

  4. C++11线程池的实现

    什么是线程池 处理大量并发任务,一个请求一个线程来处理请求任务,大量的线程创建和销毁将过多的消耗系统资源,还增加了线程上下文切换开销. 线程池通过在系统中预先创建一定数量的线程,当任务请求到来时从线程 ...

  5. c++11线程池实现

    咳咳.c++11 增加了线程库,从此告别了标准库不支持并发的历史. 然而 c++ 对于多线程的支持还是比較低级,略微高级一点的使用方法都须要自己去实现,譬如线程池.信号量等. 线程池(thread p ...

  6. c++ 11 线程池---完全使用c++ 11新特性

    前言: 目前网上的c++线程池资源多是使用老版本或者使用系统接口实现,使用c++ 11新特性的不多,最近研究了一下,实现一个简单版本,可实现任意任意参数函数的调用以及获得返回值. 0 前置知识 首先介 ...

  7. 基于C++11线程池

    1.包装线程对象 class task : public std::tr1::enable_shared_from_this<task> { public: task():exit_(fa ...

  8. 《java.util.concurrent 包源码阅读》11 线程池系列之ThreadPoolExecutor 第一部分

    先来看ThreadPoolExecutor的execute方法,这个方法能体现出一个Task被加入到线程池之后都发生了什么: public void execute(Runnable command) ...

  9. c++11线程池

    #pragma once #include <future> #include <vector> #include <atomic> #include <qu ...

随机推荐

  1. A Reliability-Aware Network Service Chain Provisioning With Delay Guarantees in NFV-Enabled Enterprise Datacenter Networks

    文章名称:A Reliability-Aware Network Service Chain Provisioning With Delay Guarantees in NFV-Enabled Ent ...

  2. Pandas系列(十)-转换连接详解

    目录 1. 拼接 1.1 append 1.2 concat 2. 关联 2.1 merge 2.2 join 数据准备 # 导入相关库 import numpy as np import panda ...

  3. 纯js Ajax 请求

    var XMLHttpReq; function createXMLHttpRequest() { if(window.ActiveXObject ) { try { XMLHttpReq = new ...

  4. 第十节:利用async和await简化异步编程模式的几种写法

    一. async和await简介 PS:简介 1. async和await这两个关键字是为了简化异步编程模型而诞生的,使的异步编程跟简洁,它本身并不创建新线程,但在该方法内部开启多线程,则另算. 2. ...

  5. Linux之搭建远程数据库[Ubuntu:全过程]

    1.mariadb在Linux中首次进入mysql (因为此时还没有创建任何用户,mysql的root并不等效于linux中的root用户) sudo apt-get install mysql-se ...

  6. 6.linux安装tomcat

    1.下载安装包 https://tomcat.apache.org/download-80.cgi       2.用 WinSCP 将本地的安装包 上传到 linux 服务器中   3.解压安装包( ...

  7. day 17 - 2 递归函数练习

    1.斐波那契   问第n个斐波那契数是多少 #这个效率是低的,最好不要使用双递归 def fib(n): if n == 1 or n == 2: return 1 return fib(n-1) + ...

  8. Spring系列(三) Bean装配的高级技术

    profile 不同于maven的profile, spring的profile不需要重新打包, 同一个版本的包文件可以部署在不同环境的服务器上, 只需要激活对应的profile就可以切换到对应的环境 ...

  9. webhook: requestbin

    A Runscope Community Project — Learn more. RequestBin Bin URL Make a request to get started. After m ...

  10. AWK如何打印从某一列到最后一列的内容

    awk -F " "  '{for (i=4;i<=NF;i++)printf("%s ", $i);print ""}' file