S.l.e!ep.¢%

像打了激速一样,以四倍的速度运转,开心的工作
简单、开放、平等的公司文化;尊重个性、自由与个人价值;
posts - 1098, comments - 335, trackbacks - 0, articles - 1
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

如何使用WINDOWS自带的线程池

Posted on 2009-02-24 20:57 S.l.e!ep.¢% 阅读(1174) 评论(0)  编辑 收藏 引用 所属分类: IOCP
【kingzai】:
Requirements
Client: Requires Windows XP or Windows 2000 Professional.
Server: Requires Windows Server 2003 or Windows 2000 Server.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Kernel32.lib.
you must add this:
#define _WIN32_WINNT 0x0500
before include windows.h

【helldream2002】:
windows核心编程里面有例子

【waterbao】:
没明白怎么用,请详细点

【kingzai】:
#define _WIN32_WINNT    0x0500

#include <cstdlib>
#include <clocale>
#include <ctime>
#include <iostream>
#include <vector>
#include <algorithm>
#include <winsock2.h>
#include <mswsock.h>

int main(int argc,char **argv)

    if(argc==2)
        DefPort=atoi(argv[1]);

    InitializeCriticalSection(&csProtection);
    SetUnhandledExceptionFilter(MyExceptionFilter);
    SetConsoleCtrlHandler(ShutdownHandler,TRUE);

    hIocp=CreateIoCompletionPort(INVALID_HANDLE_VALUE,NULL,0,0);

    WSADATA data={ 0 };
    WSAStartup(0x0202,&data);

    hListen=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if(INVALID_SOCKET==hListen)
    { 
        ShutdownHandler(0);
    }
    
    SOCKADDR_IN addr={ 0 };
    addr.sin_family=AF_INET;
    addr.sin_port=htons(DefPort);
    
    if(bind(hListen,reinterpret_cast<PSOCKADDR>(&addr),
        sizeof(addr))==SOCKET_ERROR)
    { 
        ShutdownHandler(0);
    }
    
    if(listen(hListen,256)==SOCKET_ERROR)
        ShutdownHandler(0);

    SYSTEM_INFO si={ 0 };
    GetSystemInfo(&si);
    si.dwNumberOfProcessors<<=1;

    for(int i=0;i<si.dwNumberOfProcessors;i++)
    { 
        
        QueueUserWorkItem(ThreadProc,hIocp,WT_EXECUTELONGFUNCTION);
    }
....
}

【zwzzwz】:
you must add this:
#define _WIN32_WINNT 0x0500
before include windows.h


【waterbao】:
zwzzwz() ( ) 信誉,谢谢你的回答,但是你说的include windows.h,不用手动包含这个文件呀,到底在什么位子加这个#define _WIN32_WINNT 0x0500,详细点

【kingzai】:
#define _WIN32_WINNT    0x0500

#include <cstdlib>
#include <clocale>
#include <ctime>
#include <iostream>
#include <vector>
#include <algorithm>
#include <winsock2.h>
#include <mswsock.h>


【waterbao】:
我的程序不是main()的,是MFC的,所以不知道加在那里,我把#define _WIN32_WINNT    0x0500加在使用QueueUserWorkItem(ThreadProc,hIocp,WT_EXECUTELONGFUNCTION);
这个函数的地方,编译说QueueUserWorkItem和WT_EXECUTELONGFUNCTION这个不认

【zwzzwz】:
1、在VC6下的windows.h中是没有QueueUserWorkItem的声明的。
2、VC2003的windows.h中有这个函数的定义。
你可以升级一下SDK或用VS2003

如果还不行就在stdafx.h文件的开头加入:
#ifndef WINVER
#define WINVER 0x0400
#endif

#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0500
#endi


#ifndef _WIN32_WINDOWS
#define _WIN32_WINDOWS 0x0410
#endif



【jyl168】:
mark

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理