/*Intel发布的线程池示例源代码
Win32环境的
简单看了下, 只是个基础示例, 提供设计思路而已, 效率上应该还可以进行改进
运行结果:
[Debug]
Thread Pool Method Elapsed Time: 250 ms
Four Threads Method Elapsed Time: 2422 ms
Unbound Method Elapsed Time: 2938 ms
[Release]
Thread Pool Method Elapsed Time: 250 ms
Four Threads Method Elapsed Time: 2156 ms
Unbound Method Elapsed Time: 2250 ms
共20000次线程创建
第一个结果是采用线程池的时间
第二个结果是每次4线程并行
第三个结果是直接创建20000个线程撒丫子一起跑
看来在短线程计算的情况下, 线程池的效率还是不错的
*/
// Pool.h: interface for the Pool class.
//
//////////////////////////////////////////////////////////////////////
#include <process.h>
#include <windows.h>
#include<stdio.h>
#define MAXQUEUE 15
using namespace std;
#define LOOPCOUNT 2999
class CWorkThread
{
public:
unsigned virtual ThreadExecute(){
return 0;
};
};
class Pool
{
public:
Pool();
Pool(int nMaxNumberThreads);
BOOL SubmitJob(CWorkThread* cWork);
BOOL GetWork(CWorkThread** cWork);
virtual ~Pool();
void DoWork();
void DestroyPool();
private:
//volatile long nWorkInProgress;
long nWorkInProgress;
int m_nMaxNumThreads;
unsigned int m_Thrdaddr;
HANDLE m_threadhandles[MAXQUEUE];
CWorkThread* m_pQueue[MAXQUEUE];
void *m_pParamArray[MAXQUEUE];
static unsigned _stdcall ThreadExecute(void *Param);
int m_nTopIndex;
int m_nBottomIndex;
HANDLE hEmptySlot;
HANDLE hWorkToDo;
HANDLE hExit;
CRITICAL_SECTION CriticalSection;
};
////CPP
// Pool.cpp: implementation of the Pool class.
//
//////////////////////////////////////////////////////////////////////
Pool::Pool()
{
}
Pool::Pool(int nMaxNumberThreads)
{
m_nMaxNumThreads = nMaxNumberThreads;
hEmptySlot = CreateSemaphore(NULL,MAXQUEUE,MAXQUEUE,"EmptySlot");
hWorkToDo = CreateSemaphore(NULL,0,MAXQUEUE,"WorkToDo");
hExit = CreateEvent(NULL,TRUE,FALSE,"Exit");
InitializeCriticalSection(&CriticalSection);
for(int i=0;i<m_nMaxNumThreads;i++)
{
m_threadhandles[i] = (HANDLE) _beginthreadex( NULL, 0, ThreadExecute, this, 0, &m_Thrdaddr);
}
m_nTopIndex = 0;
m_nBottomIndex = 0;
nWorkInProgress=0;
}
Pool::~Pool()
{
}
unsigned _stdcall Pool::ThreadExecute(void *Param){
((Pool*)Param)->DoWork();
return(0);
}
void Pool::DoWork()
{
CWorkThread* cWork;
while(GetWork(&cWork))
{
cWork->ThreadExecute();
InterlockedDecrement(&nWorkInProgress);
}
}//Queues up another to work
BOOL Pool::SubmitJob(CWorkThread* cWork)
{
InterlockedIncrement(&nWorkInProgress);
if(WaitForSingleObject(hEmptySlot,INFINITE) != WAIT_OBJECT_0){
return(0);
}
EnterCriticalSection(&CriticalSection);
m_pQueue[m_nTopIndex] = cWork;
m_nTopIndex = (m_nTopIndex++) % (MAXQUEUE -1);
ReleaseSemaphore(hWorkToDo,1,NULL);
LeaveCriticalSection(&CriticalSection);
return(1);
}
BOOL Pool::GetWork(CWorkThread** cWork)
{
HANDLE hWaitHandles[2];
hWaitHandles[0] = hWorkToDo;
hWaitHandles[1] = hExit;
if((WaitForMultipleObjects(2,hWaitHandles,FALSE,INFINITE) - WAIT_OBJECT_0) == 1)
return(0);
EnterCriticalSection(&CriticalSection);
CWorkThread* cWorker = m_pQueue[m_nBottomIndex];
*cWork = cWorker;
m_nBottomIndex = (m_nBottomIndex++) % (MAXQUEUE -1);
ReleaseSemaphore(hEmptySlot,1,NULL);
LeaveCriticalSection(&CriticalSection);
return(1);
}
void Pool::DestroyPool(){
while(nWorkInProgress > 0){
Sleep(10);
}
SetEvent(hExit);
DeleteCriticalSection(&CriticalSection);
}
////////////////实现
// Threadpool.cpp : Defines the entry point for the console application.
//
//#include <stdio.h>
//#include "Pool.h"
class CMyWorkThread : public CWorkThread
{
public:
unsigned virtual ThreadExecute()
{
for(int i=0;i<10;i++){
float x=4;
float y=5;
float z=34;
x= x*y/z;
z=x+34*z+y/x;
x=y*z+y/z;
}
return(0);
}
};
unsigned _stdcall ThreadProc1(void *Param){
for(int i=0;i<10;i++){
float x=4;
float y=5;
float z=34;
x= x*y/z;
z=x+34*z+y/x;
x=y*z+y/z;
Sleep(30000);
}
return(0);
}
int main(int argc, char* argv[])
{
unsigned int Thrdaddr;
int startTime;
int endTime;
HANDLE hEndEvent = CreateEvent(NULL,FALSE,TRUE,"End Event");
HANDLE hHandleArray[LOOPCOUNT] = {0};
HANDLE hSmallHandleArray[4] = {0};
startTime = GetCurrentTime ();
Pool* myPool = new Pool(4);
for(int i=0;i<LOOPCOUNT+100000;i++){
CMyWorkThread* myThread = new CMyWorkThread();
myPool->SubmitJob(myThread);
}
myPool->DestroyPool();
endTime = GetCurrentTime ();
printf( "Thread Pool Method Elapsed Time:\t%d ms\n", endTime-startTime );
ResetEvent(hEndEvent);
//startTime = GetCurrentTime ();
//for(int i=0;i<LOOPCOUNT;i+=4){
int i =1;
//hSmallHandleArray[0] = (HANDLE) _beginthreadex( NULL, 0, ThreadProc1, (void*)&i, 0, &Thrdaddr);
// hSmallHandleArray[1] = (HANDLE) _beginthreadex( NULL, 0, ThreadProc1, (void*)&i, 0, &Thrdaddr);
hSmallHandleArray[2] = (HANDLE) _beginthreadex( NULL, 0, ThreadProc1, (void*)&i, 0, &Thrdaddr);
hSmallHandleArray[2]=NULL;
// hSmallHandleArray[3] = (HANDLE) _beginthreadex( NULL, 0, ThreadProc1, (void*)&i, 0, &Thrdaddr);
WaitForSingleObject(hSmallHandleArray[2],INFINITE);
//cout<<"hello world"<<endl;
// WaitForMultipleObjects(4,hSmallHandleArray,TRUE,INFINITE);
//Sleep();
//}
//endTime=GetCurrentTime();
printf( "Four Threads Method Elapsed Time:\t%d ms\n", endTime-startTime );
//No bounds on threads
startTime = GetCurrentTime ();
for(int i=0;i<LOOPCOUNT;i++){
hHandleArray[i] = (HANDLE) _beginthreadex( NULL, 0, ThreadProc1, (void*)&i, 0, &Thrdaddr);
}
WaitForMultipleObjects(LOOPCOUNT,hHandleArray,TRUE,INFINITE);
endTime = GetCurrentTime();
printf( "Unbound Method Elapsed Time:\t\t%d ms\n", endTime-startTime );
system("pause");
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
WINDOWS完成端口编程IOCP&&THREADPOOL
共4个文件
rar:2个
doc:1个
cpp:1个
5星 · 超过95%的资源 需积分: 10 23 下载量 136 浏览量
2013-04-27
21:14:35
上传
评论 2
收藏 2.53MB RAR 举报
温馨提示
WINDOWS完成端口编程 1、基本概念 2、WINDOWS完成端口的特点 3、完成端口(Completion Ports )相关数据结构和创建 4、完成端口线程的工作原理 5、Windows完成端口的实例代码
资源推荐
资源详情
资源评论
收起资源包目录
IOCP&&THREADPOOL.rar (4个子文件)
IOCP_原理_代码.doc 232KB
IOCP完成端口详解(pdf文档%2B源码).rar 429KB
Intel发布的线程池示例源代码.cpp 6KB
博客文章《完成端口详解》配套代码.rar 2.06MB
共 4 条
- 1
资源评论
- 贝壳里的沙2014-07-13很不错的资源,具有参考价值
- IDEAFLY2014-03-22还可以,有一定参考价值
- Aoliqing2013-08-26资料挺全面,学习中。。。
D-T
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 为 Go 自动生成的 Google API .zip
- 一组快速入门示例,演示了适用于 Android 和 iOS 的 Google API.zip
- 一款简单但有效的 Go 网站迷你分析器.zip
- 一个线程安全的并发映射.zip
- 一个用于与任意 JSON 交互的 Go 包.zip
- 一个用于 go 的 cron 库.zip
- 基于BJUI + Spring MVC + Spring + Mybatis框架的办公自动化系统设计源码
- 基于百度地图的Java+HTML+JavaScript+CSS高速公路设备管理系统设计源码
- 基于Django Web框架的母婴商城实践项目设计源码
- 一个使用 Go 编程语言和 WebAssembly 构建渐进式 Web 应用程序的包 .zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功