window下的线程池
来源:互联网 发布:最新淘宝达人申请 编辑:程序博客网 时间:2024/04/28 07:26
// 线程任务基类和线程池基类文件#ifndef _ITHREADPOOL_H#define _ITHREADPOOL_Hclass IHandle{public: IHandle(){} virtual ~IHandle(){}public: virtual void Run() = 0;};class IThreadPool{public: IThreadPool(){} virtual ~IThreadPool(){}public: virtual void SetMaxNum(int iMaxNum) = 0;public: virtual int Start() = 0; virtual void Stop() = 0;public: virtual int Dispatch(IHandle *pHandle) = 0;};#endif// 线程池实现类头文件#ifndef _THREADPOOL_H#define _THREADPOOL_H#include <Windows.h>#include "CircleQueue.h"#include "IThreadPool.h"class CThreadPool : public IThreadPool{ public: CThreadPool(); virtual ~CThreadPool();public: void SetMaxNum(int iMaxNum);public: int Start(); void Stop();public: int Dispatch(IHandle *pHandle);private: static DWORD WINAPI ThreadFun(LPVOID lParam);private: bool m_bIsRun; int m_iMaxNum; HANDLE m_hStopEvent; CircleQueue m_Queue; CRITICAL_SECTION m_CS; CONDITION_VARIABLE m_ConVar;};#endif// 线程池 实现类源文件#include "ThreadPool.h"#include <iostream>using namespace std;const int OK = 0;const int ERR = -1;CThreadPool::CThreadPool() : m_bIsRun(false),m_iMaxNum(0){}CThreadPool::~CThreadPool(){}void CThreadPool::SetMaxNum(int iMaxNum){ m_iMaxNum = iMaxNum;}int CThreadPool::Start(){ int iRev = ERR; // 初始化 m_bIsRun = true; InitializeConditionVariable(&m_ConVar); InitializeCriticalSection(&m_CS); // 创建线程 HANDLE hThread = NULL; for (int i = 0; i < m_iMaxNum; i++) { hThread = CreateThread(NULL, 0, ThreadFun, this, 0, NULL); CloseHandle(hThread); } // 创建停止事件 m_hStopEvent = CreateEvent(NULL, FALSE, FALSE, NULL); if (m_hStopEvent != NULL) { iRev = OK; } return iRev;}void CThreadPool::Stop(){ m_bIsRun = false; WakeAllConditionVariable(&m_ConVar); WaitForSingleObject(m_hStopEvent, INFINITE); CloseHandle(m_hStopEvent); m_hStopEvent = NULL; DeleteCriticalSection(&m_CS);}int CThreadPool::Dispatch(IHandle *pHandle){ EnterCriticalSection(&m_CS); m_Queue.push(pHandle); LeaveCriticalSection(&m_CS); WakeConditionVariable(&m_ConVar); return 0;}DWORD WINAPI CThreadPool::ThreadFun(LPVOID lParam){ IHandle *pIHandle = NULL; CThreadPool *pThreadPool = (CThreadPool *)lParam; while(pThreadPool->m_bIsRun) { EnterCriticalSection(&pThreadPool->m_CS); while(pThreadPool->m_Queue.getLength() == 0) { SleepConditionVariableCS(&pThreadPool->m_ConVar, &pThreadPool->m_CS, INFINITE); if (!pThreadPool->m_bIsRun) break; } pIHandle = (IHandle *)pThreadPool->m_Queue.pop(); LeaveCriticalSection(&pThreadPool->m_CS); if (!pThreadPool->m_bIsRun) { delete pIHandle; break; } if (pIHandle != NULL) { pIHandle->Run(); delete pIHandle; } } EnterCriticalSection(&pThreadPool->m_CS); if (--pThreadPool->m_iMaxNum == 0) { SetEvent(pThreadPool->m_hStopEvent); cout << pThreadPool->m_iMaxNum << endl; } LeaveCriticalSection(&pThreadPool->m_CS); return 0;}// 这里还使用到一个环形队列// 头文件#ifndef _CIRCLEQUEUE_H#define _CIRCLEQUEUE_Hclass CircleQueue {public: CircleQueue(); virtual ~CircleQueue();public: void push(void * item); void * pop(); void * top(); int getLength();private: void ** mEntries; unsigned int mHead; // 可以读的位置 unsigned int mTail; // 可以写的位置 unsigned int mCount; // 元素的个数 unsigned int mMaxCount; // 总的数量};#endif// 环形队列源文件#include "CircleQueue.h"#include "stdlib.h"#include "memory.h"CircleQueue :: CircleQueue(){ mMaxCount = 8; mEntries = (void**)malloc( sizeof( void * ) * mMaxCount ); mHead = mTail = mCount = 0;}CircleQueue :: ~CircleQueue(){ free( mEntries ); mEntries = NULL;}void CircleQueue :: push( void * item ){ if( mCount >= mMaxCount ) { mMaxCount = ( mMaxCount * 3 ) / 2 + 1; void ** newEntries = (void**)malloc( sizeof( void * ) * mMaxCount ); unsigned int headLen = 0, tailLen = 0; if( mHead < mTail ) { headLen = mTail - mHead; } else { headLen = mCount - mTail; tailLen = mTail; } memcpy( newEntries, &( mEntries[ mHead ] ), sizeof( void * ) * headLen ); if( tailLen ) { memcpy( &( newEntries[ headLen ] ), mEntries, sizeof( void * ) * tailLen ); } mHead = 0; mTail = headLen + tailLen; free( mEntries ); mEntries = newEntries; } mEntries[ mTail++ ] = item; mTail = mTail % mMaxCount; mCount++;}void * CircleQueue :: pop(){ void * ret = NULL; if( mCount > 0 ) { ret = mEntries[ mHead++ ]; mHead = mHead % mMaxCount; mCount--; } return ret;}void * CircleQueue :: top(){ return mCount > 0 ? mEntries[ mHead ] : NULL;}int CircleQueue :: getLength(){ return mCount;}// main 函数实现 这里没有删除对象 #include <iostream>#include "ThreadPool.h"using namespace std;class TestHandle : public IHandle{public: virtual void Run() { cout << GetCurrentThreadId() << "---------"; cout << "Hello" << endl; }};int main(){ CThreadPool threadPool; threadPool.SetMaxNum(10); threadPool.Start(); for (int i = 0; i < 100; i++) { threadPool.Dispatch(new TestHandle()); } Sleep(1000*5); threadPool.Stop(); return 0;}
0 0
- window下的线程池
- window下创建线程的API方法
- window下创建线程的API方法
- window下C++线程池实现
- Window下C线程
- Window下进程和线程的关系【转】
- window线程池
- linux和window下线程同步类
- Window下的常用命令
- Window下的命令
- window下的netstat
- window下线程同步之(Event Objects(事件))的具体使用和说明
- 在window环境下,实现类似std::thread的线程类
- window下线程同步之(Event Objects(事件)) / 事件CEvent的使用
- Window线程
- window查看使用cpu过高的线程
- firefox下的window.event
- window下memcache的安装
- 嵌入式 linux中probe函数传递参数的寻找(下)
- 获得java.lang.Throwable错误堆栈信息
- ios自定义类(UIView)代码生成简单的UITableViewCell
- 2014-09-30《尘埃落定》
- Python快速教程
- window下的线程池
- Debian7离线升级bash漏洞—再修复方法
- [leetcode]Sort Colors
- 嵌入式 linux中kernel代码/lib/klist.c文件分析
- Linux系统平台下关于GCC编译及使用的方法(-Werror,它要求GCC将所有的警告当成错误进行处理 )
- JAVA学习——Servlet
- CC2530与zigbee学习笔记の协调器、路由器、终端的概念
- Java知识总结
- 正则表达式(转)