使用QueueUserWorkerItem实现的线程池封装
此线程池所依赖的线程类,请参看《一个Windows C++的线程类实现》:
http://blog.csdn.net/huyiyang2010/archive/2010/08/10/5801597.aspx
SystemThreadPool.h
- #define __SYSTEM_THREAD_POOL__
- #include "Thread.h"
- #include <list>
- #include <windows.h>
- class CThreadPoolExecutor
- {
- public:
- CThreadPoolExecutor(void);
- ~CThreadPoolExecutor(void);
- /**
- 初始化线程池,创建minThreads个线程
- **/
- bool Init(unsigned int maxTaskse);
- /**
- 执行任务,若当前任务列表没有满,将此任务插入到任务列表,返回true
- 否则返回false
- **/
- bool Execute(Runnable * pRunnable);
- /**
- 终止线程池,先制止塞入任务,
- 然后等待直到任务列表为空,
- 然后设置最小线程数量为0,
- 等待直到线程数量为空,
- 清空垃圾堆中的任务
- **/
- void Terminate();
- /**
- 返回线程池中当前的线程数量
- **/
- unsigned int GetThreadPoolSize();
- private:
- static unsigned int WINAPI StaticThreadFunc(void * arg);
- private:
- typedef std::list<Runnable *> Tasks;
- typedef Tasks::iterator TasksItr;
- Tasks m_Tasks;
- CRITICAL_SECTION m_csTasksLock;
- volatile bool m_bRun;
- volatile bool m_bEnableInsertTask;
- volatile unsigned int m_maxTasks;
- };
- #endif
SytemThreadPool.cpp
- #include "SystemThreadPool.h"
- CThreadPoolExecutor::CThreadPoolExecutor(void) :
- m_bRun(false),
- m_bEnableInsertTask(false)
- {
- InitializeCriticalSection(&m_csTasksLock);
- }
- CThreadPoolExecutor::~CThreadPoolExecutor(void)
- {
- Terminate();
- DeleteCriticalSection(&m_csTasksLock);
- }
- bool CThreadPoolExecutor::Init(unsigned int maxTasks)
- {
- if(maxTasks == 0)
- {
- return false;
- }
- m_maxTasks = maxTasks;
- m_bRun = true;
- m_bEnableInsertTask = true;
- return true;
- }
- bool CThreadPoolExecutor::Execute(Runnable * pRunnable)
- {
- if(!m_bEnableInsertTask)
- {
- return false;
- }
- if(NULL == pRunnable)
- {
- return false;
- }
- EnterCriticalSection(&m_csTasksLock);
- if(m_Tasks.size() >= m_maxTasks)
- {
- LeaveCriticalSection(&m_csTasksLock);
- return false;
- }
- m_Tasks.push_back(pRunnable);
- LeaveCriticalSection(&m_csTasksLock);
- bool ret = QueueUserWorkItem((LPTHREAD_START_ROUTINE)StaticThreadFunc, this, WT_EXECUTEINPERSISTENTIOTHREAD);
- if(!ret)
- {
- EnterCriticalSection(&m_csTasksLock);
- m_Tasks.remove(pRunnable);
- LeaveCriticalSection(&m_csTasksLock);
- }
- return ret;
- }
- unsigned int CThreadPoolExecutor::GetThreadPoolSize()
- {
- return m_Tasks.size();
- }
- void CThreadPoolExecutor::Terminate()
- {
- m_bEnableInsertTask = false;
- m_bRun = false;
- while(m_Tasks.size() != 0)
- {
- Sleep(1);
- }
- }
- unsigned int WINAPI CThreadPoolExecutor::StaticThreadFunc(void * arg)
- {
- CThreadPoolExecutor * pThreadPool = (CThreadPoolExecutor *)arg;
- Runnable * pRunnable = NULL;
- EnterCriticalSection(&pThreadPool->m_csTasksLock);
- pRunnable = pThreadPool->m_Tasks.front();
- if(NULL != pRunnable)
- {
- pThreadPool->m_Tasks.pop_front();
- }
- LeaveCriticalSection(&pThreadPool->m_csTasksLock);
- if(NULL != pRunnable)
- {
- pRunnable->Run();
- }
- return 0;
- }
用法:
#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实现的线程池封装的更多相关文章
- jedis使用线程池封装redis基本操作
redisclient jedis 经常使用的 操作 key value hash list set zset 的基本操作 package cn.zto.util; import java.util. ...
- Java开发笔记(一百零四)普通线程池的运用
前面介绍了线程的基本用法,以及多线程并发的问题处理,但实际开发中往往存在许多性质相似的任务,比如批量发送消息.批量下载文件.批量进行交易等等.这些同类任务的处理流程一致,不存在资源共享问题,相互之间也 ...
- Java ExecutorService四种线程池及自定义ThreadPoolExecutor机制
一.Java 线程池 Java通过Executors提供四种线程池,分别为:1.newCachedThreadPool:创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收 ...
- 谈谈Java的线程池设计
在实际项目中,如果因为想异步执行暂时性的任务而不断创建线程是很浪费资源的事情(当一个任务执行完后,线程也没用了).这种情况下,最好是将任务提交给线程池执行. 所谓池,就是将管理某一种资源,对资源进行复 ...
- 进程池与线程池基本使用、协程理论与实操、IO模型、前端、BS架构、HTTP协议与HTML前戏
昨日内容回顾 GIL全局解释器锁 1.在python解释器中 才有GIL的存在(只与解释器有关) 2.GIL本质上其实也是一把互斥锁(并发变串行 牺牲效率保证安全) 3.GIL的存在 是由于Cpyth ...
- Android AsyncTask 深度理解、简单封装、任务队列分析、自定义线程池
前言:由于最近在做SDK的功能,需要设计线程池.看了很多资料不知道从何开始着手,突然发现了AsyncTask有对线程池的封装,so,就拿它开刀,本文将从AsyncTask的基本用法,到简单的封装,再到 ...
- c++封装编写线程池
在csapp学习或者其他linux底层编程的过程中,一般都会举一些多线程或多进程的例子,配合底层同步原语.系统调用api来解释怎么创建多线程/多进程. 但是这些例子和实际项目中所用到的多线程/多进程编 ...
- 面向对象的线程池Threadpool的封装
线程池是一种多线程处理形式,预先创建好一定数量的线程,将其保存于一个容器中(如vector), 处理过程中将任务添加到队列,然后从容器中取出线程后自动启动这些任务,具体实现如下. 以下是UML图,展示 ...
- 线程池、及使用场景、线程安全封装、ConcurrentHashMap应用场景
https://blog.csdn.net/sinbadfreedom/article/details/80467253 :1.HashMap与ConcurrentHashMap的区别与应用场景 h ...
随机推荐
- ZRender源码分析5:Shape绘图详解
回顾 上一篇说到:ZRender源码分析4:Painter(View层)-中,这次,来补充一下具体的shape 关于热区的边框 以圆形为例: document.addEventListener('DO ...
- ArrayList--卧槽这就是源码
最近在<数据结构与算法分析(java版)>中看到了实现ArrayList的简易代码,亲自做了一下.个中的疑点还是在代码中实现了一下.其中就包括内部类Iterator对外部成员访问的问题. ...
- popen()函数详解
popen()函数 /*============================================ > Copyright (C) 2014 All rights reserved ...
- qt运行库
KERNEL32.DLL MINGWM10.DLL MSVCRT.DLL LIBGCC_S_DW2-1.DLL QTCORE4.DLL QTGUI4.DLL 笔者安装的是QT SDK.(发行版本这是前 ...
- java调用shell脚本
/** * 运行shell脚本 * @param shell 需要运行的shell脚本 */ public static void execShell(String shell){ try { Run ...
- 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 ...
- SQL Cast()函数
sql cast()函数 2010-09-17 13:30:26| 分类: Sql | 标签:sql case() 函数 |字号大中小 订阅 (1).CAST()函数的参数是一个表达式,它包括用AS关 ...
- “entities.LastOrDefault()”引发了类型“System.NotSupportedException”的异常
问题: var entities = new ShipuPlanBLO().UserList(userId, beginDate, endDate); DateTime maxDate = entit ...
- User cannot be resolved to a type
出现 User cannot be resolved to a type 不知道具体问题出在哪里但是我经过将全路径输入并保存后错误消失 将User选上,然后点击保存就可以了! 最后我发现错误消失了!
- poj2121--暴力解法
#include<iostream> #include<string> using namespace std; ]={"negative","z ...