也可参考:  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. Java使用DOM4J对XML文件进行增删改查操作

    Java进行XML文件操作,代码如下: package com.founder.mrp.util; import java.io.File; import java.util.ArrayList; i ...

  2. 优化算法系列-遗传算法(3)——NSGAII学习网址

    JMetal https://www.cnblogs.com/denggaoshan/p/6306640.html https://www.cnblogs.com/denggaoshan/p/6308 ...

  3. crm 权限设计

    先在项目中创建  app rbac的models.py from django.db import models class Permission(models.Model): "" ...

  4. JQuery对象关系图

    上图转自:http://www.cnblogs.com/haogj/archive/2010/04/19/1715762.html 自定义函数示例: $.fn.jAccordionunfold = f ...

  5. MacOS下好用的截图软件snip

    1 官网下载,腾讯出的 https://snip.qq.com/ 2 下一步下一步安装就好,然后设置一下自己喜欢的快捷键,我的是command + control+J,选择自己喜欢的或者默认都可以 3 ...

  6. Django REST Framework API Guide 06

    本节大纲 1.Validators 2.Authentication Validators 在REST框架中处理验证的大多数时间,您将仅仅依赖于缺省字段验证,或在序列化器或字段类上编写显式验证方法.但 ...

  7. POJ 1269 Intersecing Lines (直线相交)

    题目: Description We all know that a pair of distinct points on a plane defines a line and that a pair ...

  8. JQuery EasyUI 表单

    EasyUI 创建异步提交表单 ♦ 通过使用 easyui 表单插件来改变表单为 ajax表单. 表单提交所有字段到后台服务器,服务器处理和发送一些数据返回到前端页面.我们接收返回数据,并将它显示出来 ...

  9. WIFI的AP/Sta模式简单介绍

    AP模式: Access Point,提供无线接入服务,允许其它无线设备接入,提供数据访问,一般的无线路由/网桥工作在该模式下.AP和AP之间允许相互连接 Sta模式: Station, 类似于无线终 ...

  10. 【python】实用的logging封装

    #!/usr/bin/python import logging import logging.handlers def set_logger(filename, logmod): log_size ...