最近由于项目需要正在研究完成端口,写了不少Demo程序,我发现完成端口并不能控制并发线程的数量即CreateIoCompletionPort函数的第四个参数NumberOfConcurrentThreads无论设置成什么数都没有任何效果,这个参数很重要要是不能控制并发的线程数必然导致大量的线程切换影响性能。请各位老鸟们百忙中抽空看下希望能解答我的这个疑问。以下是一Demo程序的源码:
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <conio.h>DWORD BeginTime;int compute()
{
srand(BeginTime);
for(int i=0; i<20 *1000 * 10000; i++)
rand();
return rand();
}void WorkItem(LPVOID lpParameter)
{
DWORD szTransfer,key;
HANDLE hIO = (HANDLE)lpParameter;
LPOVERLAPPED pOL;
while(GetQueuedCompletionStatus(hIO,&szTransfer,&key,&pOL,INFINITE))
{
if(szTransfer)
{
printf("[0x%x] compute in!\n",GetCurrentThreadId());
compute();
printf("[0x%x] compute out!\n",GetCurrentThreadId());
}
else
break;
}
}int main(int argc, char* argv[])
{
BOOL br; printf("[0x%x]Hello World!\n",GetCurrentThreadId());
//创建完成端口并设置最大并发线程数为1
HANDLE hIO = CreateIoCompletionPort(INVALID_HANDLE_VALUE,0,0,1);
//创建2个线程
unsigned long hT1 = _beginthread(WorkItem,0,hIO);
unsigned long hT2 = _beginthread(WorkItem,0,hIO);
//确保线程处于挂起状态即运行至GetQueuedCompletionStatus
Sleep(1000);
//发送6个IO消息
br = PostQueuedCompletionStatus(hIO,TRUE,0,NULL);
br = PostQueuedCompletionStatus(hIO,TRUE,0,NULL);
br = PostQueuedCompletionStatus(hIO,TRUE,0,NULL);
br = PostQueuedCompletionStatus(hIO,TRUE,0,NULL);
br = PostQueuedCompletionStatus(hIO,TRUE,0,NULL);
br = PostQueuedCompletionStatus(hIO,TRUE,0,NULL);
//发送2个结束消息
br = PostQueuedCompletionStatus(hIO,FALSE,0,NULL);
br = PostQueuedCompletionStatus(hIO,FALSE,0,NULL);
//等待线程退出
WaitForSingleObject((HANDLE)hT1,INFINITE);
WaitForSingleObject((HANDLE)hT2,INFINITE);
CloseHandle(hIO);
getch();
return 0;
}
运行结果:
[0x161c]Hello World!
[0x1630] compute in!
[0x1628] compute in!
[0x1630] compute out!
[0x1630] compute in!
[0x1628] compute out!
[0x1628] compute in!
[0x1630] compute out!
[0x1630] compute in!
[0x1628] compute out!
[0x1628] compute in!
[0x1630] compute out!
[0x1628] compute out!
并发的线程是2个
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <conio.h>DWORD BeginTime;int compute()
{
srand(BeginTime);
for(int i=0; i<20 *1000 * 10000; i++)
rand();
return rand();
}void WorkItem(LPVOID lpParameter)
{
DWORD szTransfer,key;
HANDLE hIO = (HANDLE)lpParameter;
LPOVERLAPPED pOL;
while(GetQueuedCompletionStatus(hIO,&szTransfer,&key,&pOL,INFINITE))
{
if(szTransfer)
{
printf("[0x%x] compute in!\n",GetCurrentThreadId());
compute();
printf("[0x%x] compute out!\n",GetCurrentThreadId());
}
else
break;
}
}int main(int argc, char* argv[])
{
BOOL br; printf("[0x%x]Hello World!\n",GetCurrentThreadId());
//创建完成端口并设置最大并发线程数为1
HANDLE hIO = CreateIoCompletionPort(INVALID_HANDLE_VALUE,0,0,1);
//创建2个线程
unsigned long hT1 = _beginthread(WorkItem,0,hIO);
unsigned long hT2 = _beginthread(WorkItem,0,hIO);
//确保线程处于挂起状态即运行至GetQueuedCompletionStatus
Sleep(1000);
//发送6个IO消息
br = PostQueuedCompletionStatus(hIO,TRUE,0,NULL);
br = PostQueuedCompletionStatus(hIO,TRUE,0,NULL);
br = PostQueuedCompletionStatus(hIO,TRUE,0,NULL);
br = PostQueuedCompletionStatus(hIO,TRUE,0,NULL);
br = PostQueuedCompletionStatus(hIO,TRUE,0,NULL);
br = PostQueuedCompletionStatus(hIO,TRUE,0,NULL);
//发送2个结束消息
br = PostQueuedCompletionStatus(hIO,FALSE,0,NULL);
br = PostQueuedCompletionStatus(hIO,FALSE,0,NULL);
//等待线程退出
WaitForSingleObject((HANDLE)hT1,INFINITE);
WaitForSingleObject((HANDLE)hT2,INFINITE);
CloseHandle(hIO);
getch();
return 0;
}
运行结果:
[0x161c]Hello World!
[0x1630] compute in!
[0x1628] compute in!
[0x1630] compute out!
[0x1630] compute in!
[0x1628] compute out!
[0x1628] compute in!
[0x1630] compute out!
[0x1630] compute in!
[0x1628] compute out!
[0x1628] compute in!
[0x1630] compute out!
[0x1628] compute out!
并发的线程是2个
解决方案 »
- error LNK2019和error LNK1120如何解决?!急求!!
- 如何获取window提示框的按钮消息
- 按钮操作怎么实现
- vss联不上的问题。
- 属性只读的问题!
- How can i get a IHTMLDOcument interface from a HWND ?
- 一个不错的 VC/C/C++ 书籍下载站. UP有分!
- 在线等待!奇怪的问题:如何更新帮助?
- vc操作数据库比其他语言有什么优势呢?
- 怎样利用OCX的Method中的参数传回处理后的结果
- 谁能帮我实现WaveOut的循环播放和停止播放功能?有代码,不懂WaveOut也可以过来学习一下。
- 关于串口通信程序问题(vc++ 6.0,MScom控件)
来自MSDN:
Maximum number of threads that the operating system allows to concurrently process I/O completion packets for the I/O completion port. If this parameter is zero, the system allows as many concurrently running threads as there are processors in the system.