此线程池所依赖的线程类,请参看《一个Windows C++的线程类实现》:

http://blog.csdn.net/huyiyang2010/archive/2010/08/10/5801597.aspx

SystemThreadPool.h

  1. #define __SYSTEM_THREAD_POOL__
  2. #include "Thread.h"
  3. #include <list>
  4. #include <windows.h>
  5. class CThreadPoolExecutor
  6. {
  7. public:
  8. CThreadPoolExecutor(void);
  9. ~CThreadPoolExecutor(void);
  10. /**
  11. 初始化线程池,创建minThreads个线程
  12. **/
  13. bool Init(unsigned int maxTaskse);
  14. /**
  15. 执行任务,若当前任务列表没有满,将此任务插入到任务列表,返回true
  16. 否则返回false
  17. **/
  18. bool Execute(Runnable * pRunnable);
  19. /**
  20. 终止线程池,先制止塞入任务,
  21. 然后等待直到任务列表为空,
  22. 然后设置最小线程数量为0,
  23. 等待直到线程数量为空,
  24. 清空垃圾堆中的任务
  25. **/
  26. void Terminate();
  27. /**
  28. 返回线程池中当前的线程数量
  29. **/
  30. unsigned int GetThreadPoolSize();
  31. private:
  32. static unsigned int WINAPI StaticThreadFunc(void * arg);
  33. private:
  34. typedef std::list<Runnable *> Tasks;
  35. typedef Tasks::iterator TasksItr;
  36. Tasks m_Tasks;
  37. CRITICAL_SECTION m_csTasksLock;
  38. volatile bool m_bRun;
  39. volatile bool m_bEnableInsertTask;
  40. volatile unsigned int m_maxTasks;
  41. };
  42. #endif

SytemThreadPool.cpp

  1. #include "SystemThreadPool.h"
  2. CThreadPoolExecutor::CThreadPoolExecutor(void) :
  3. m_bRun(false),
  4. m_bEnableInsertTask(false)
  5. {
  6. InitializeCriticalSection(&m_csTasksLock);
  7. }
  8. CThreadPoolExecutor::~CThreadPoolExecutor(void)
  9. {
  10. Terminate();
  11. DeleteCriticalSection(&m_csTasksLock);
  12. }
  13. bool CThreadPoolExecutor::Init(unsigned int maxTasks)
  14. {
  15. if(maxTasks == 0)
  16. {
  17. return false;
  18. }
  19. m_maxTasks = maxTasks;
  20. m_bRun = true;
  21. m_bEnableInsertTask = true;
  22. return true;
  23. }
  24. bool CThreadPoolExecutor::Execute(Runnable * pRunnable)
  25. {
  26. if(!m_bEnableInsertTask)
  27. {
  28. return false;
  29. }
  30. if(NULL == pRunnable)
  31. {
  32. return false;
  33. }
  34. EnterCriticalSection(&m_csTasksLock);
  35. if(m_Tasks.size() >= m_maxTasks)
  36. {
  37. LeaveCriticalSection(&m_csTasksLock);
  38. return false;
  39. }
  40. m_Tasks.push_back(pRunnable);
  41. LeaveCriticalSection(&m_csTasksLock);
  42. bool ret = QueueUserWorkItem((LPTHREAD_START_ROUTINE)StaticThreadFunc, this, WT_EXECUTEINPERSISTENTIOTHREAD);
  43. if(!ret)
  44. {
  45. EnterCriticalSection(&m_csTasksLock);
  46. m_Tasks.remove(pRunnable);
  47. LeaveCriticalSection(&m_csTasksLock);
  48. }
  49. return ret;
  50. }
  51. unsigned int CThreadPoolExecutor::GetThreadPoolSize()
  52. {
  53. return m_Tasks.size();
  54. }
  55. void CThreadPoolExecutor::Terminate()
  56. {
  57. m_bEnableInsertTask = false;
  58. m_bRun = false;
  59. while(m_Tasks.size() != 0)
  60. {
  61. Sleep(1);
  62. }
  63. }
  64. unsigned int WINAPI CThreadPoolExecutor::StaticThreadFunc(void * arg)
  65. {
  66. CThreadPoolExecutor * pThreadPool = (CThreadPoolExecutor *)arg;
  67. Runnable * pRunnable = NULL;
  68. EnterCriticalSection(&pThreadPool->m_csTasksLock);
  69. pRunnable = pThreadPool->m_Tasks.front();
  70. if(NULL != pRunnable)
  71. {
  72. pThreadPool->m_Tasks.pop_front();
  73. }
  74. LeaveCriticalSection(&pThreadPool->m_csTasksLock);
  75. if(NULL != pRunnable)
  76. {
  77. pRunnable->Run();
  78. }
  79. return 0;
  80. }

用法:

#include "Thread.h"
#include "SystemThreadPool.h"

class R : public Runnable
{
public:
    ~R()
    {
    }
    void Run()
    {
        printf("Hello World/n");
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    CThreadPoolExecutor * pExecutor = new CThreadPoolExecutor();
    pExecutor->Init(50);
    R r;
    for(int i=0;i<100;i++)
    {
        while(!pExecutor->Execute(&r))
        {
        }
    }
    pExecutor->Terminate();
    delete pExecutor;
    getchar();
    return 0;
}

测试结果:

机器:

Intel(R) Core(TM)2 Duo CPU

E8400 @ 3.00GHz

2G内存

对于100个任务并且每个任务包含10000000个循环,任务中无等待:

线程池耗时:2203时间片

from:http://blog.csdn.net/huyiyang2010/article/details/5820548

使用QueueUserWorkerItem实现的线程池封装的更多相关文章

  1. jedis使用线程池封装redis基本操作

    redisclient jedis 经常使用的 操作 key value hash list set zset 的基本操作 package cn.zto.util; import java.util. ...

  2. Java开发笔记(一百零四)普通线程池的运用

    前面介绍了线程的基本用法,以及多线程并发的问题处理,但实际开发中往往存在许多性质相似的任务,比如批量发送消息.批量下载文件.批量进行交易等等.这些同类任务的处理流程一致,不存在资源共享问题,相互之间也 ...

  3. Java ExecutorService四种线程池及自定义ThreadPoolExecutor机制

    一.Java 线程池 Java通过Executors提供四种线程池,分别为:1.newCachedThreadPool:创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收 ...

  4. 谈谈Java的线程池设计

    在实际项目中,如果因为想异步执行暂时性的任务而不断创建线程是很浪费资源的事情(当一个任务执行完后,线程也没用了).这种情况下,最好是将任务提交给线程池执行. 所谓池,就是将管理某一种资源,对资源进行复 ...

  5. 进程池与线程池基本使用、协程理论与实操、IO模型、前端、BS架构、HTTP协议与HTML前戏

    昨日内容回顾 GIL全局解释器锁 1.在python解释器中 才有GIL的存在(只与解释器有关) 2.GIL本质上其实也是一把互斥锁(并发变串行 牺牲效率保证安全) 3.GIL的存在 是由于Cpyth ...

  6. Android AsyncTask 深度理解、简单封装、任务队列分析、自定义线程池

    前言:由于最近在做SDK的功能,需要设计线程池.看了很多资料不知道从何开始着手,突然发现了AsyncTask有对线程池的封装,so,就拿它开刀,本文将从AsyncTask的基本用法,到简单的封装,再到 ...

  7. c++封装编写线程池

    在csapp学习或者其他linux底层编程的过程中,一般都会举一些多线程或多进程的例子,配合底层同步原语.系统调用api来解释怎么创建多线程/多进程. 但是这些例子和实际项目中所用到的多线程/多进程编 ...

  8. 面向对象的线程池Threadpool的封装

    线程池是一种多线程处理形式,预先创建好一定数量的线程,将其保存于一个容器中(如vector), 处理过程中将任务添加到队列,然后从容器中取出线程后自动启动这些任务,具体实现如下. 以下是UML图,展示 ...

  9. 线程池、及使用场景、线程安全封装、ConcurrentHashMap应用场景

    https://blog.csdn.net/sinbadfreedom/article/details/80467253  :1.HashMap与ConcurrentHashMap的区别与应用场景 h ...

随机推荐

  1. ZRender源码分析5:Shape绘图详解

    回顾 上一篇说到:ZRender源码分析4:Painter(View层)-中,这次,来补充一下具体的shape 关于热区的边框 以圆形为例: document.addEventListener('DO ...

  2. ArrayList--卧槽这就是源码

    最近在<数据结构与算法分析(java版)>中看到了实现ArrayList的简易代码,亲自做了一下.个中的疑点还是在代码中实现了一下.其中就包括内部类Iterator对外部成员访问的问题. ...

  3. popen()函数详解

    popen()函数 /*============================================ > Copyright (C) 2014 All rights reserved ...

  4. qt运行库

    KERNEL32.DLL MINGWM10.DLL MSVCRT.DLL LIBGCC_S_DW2-1.DLL QTCORE4.DLL QTGUI4.DLL 笔者安装的是QT SDK.(发行版本这是前 ...

  5. java调用shell脚本

    /** * 运行shell脚本 * @param shell 需要运行的shell脚本 */ public static void execShell(String shell){ try { Run ...

  6. leetcode_question_111 Minimum Depth of Binary Tree

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  7. SQL Cast()函数

    sql cast()函数 2010-09-17 13:30:26| 分类: Sql | 标签:sql case() 函数 |字号大中小 订阅 (1).CAST()函数的参数是一个表达式,它包括用AS关 ...

  8. “entities.LastOrDefault()”引发了类型“System.NotSupportedException”的异常

    问题: var entities = new ShipuPlanBLO().UserList(userId, beginDate, endDate); DateTime maxDate = entit ...

  9. User cannot be resolved to a type

    出现 User cannot be resolved to a type 不知道具体问题出在哪里但是我经过将全路径输入并保存后错误消失 将User选上,然后点击保存就可以了! 最后我发现错误消失了!

  10. poj2121--暴力解法

    #include<iostream> #include<string> using namespace std; ]={"negative","z ...