select实现高精度定时器
(2011-08-04 16:45:03)
标签:
selecttimer |
分类: windows |
select实现高精度定时器
此博文禁止转载,谢谢
因为各种各样的原因,有时候必须把超时和socket的事件放在一个线程里,也就是说不可避免的要用select做超时功能,
经过测试,select在windows下的timeout功能精度太低,大概在15ms左右,在linux下精度在1ms,我记得几年前就是这种情况,几年过去了,MS还是这个德性,一点长进都没有。
select函数本身限制够多的,比如可查询的handle数,linux下不重新编译内核的话,是1024,但是linux下有没有这个限制,而且性能更高的函数可选择。windows根本就没辙,就这样了,自己想点其他办法。
因为这个原因,几年前毅然选择了linux。
但是现在没辙了,大环境在windows,只能想点歪门邪道了。
在windows下,经测试select超时精度在15ms,timeval.tv_usec设为1,1000,10000,都一样,14ms多超时,说明精度在15ms,但是为0的话,一般10us内超时。
但是我的要求是select的精度在1ms。得想办法来。
select的句柄有事件的话,select的返回挺快的,这是不是有可利用的地方,答案就是这个。
create两个dummy handle与windows的1ms精度的多媒体定时器配合使用,使select超时达到1ms的精度。
伪代码如下:
1. create ReadDummySocket
2. bind
3. create WriteDummySocket
4. call timeGetDevCaps() get windows multimedia timer
resolution
5.
5.1 calculate timeout for select() function;
5.2 call timeSetEvent() function to set a windows multimedia
timer.
5.3 add ReadDummySocket to readset fdset, and call select() to
query handle's events or timeout
5.4 send a dummy packet to ReadDummySocket through WriteDummySocket
in the CALLBACK functin of windows multimedia timer
5.5 select return when ReadDummySocket has a readable event
5.6 call timeKillEvent() to kill the windows multimedia timer id
return in step 5.2
parts of .h file
int dummyReadSocket;
int dummyWriteSocket;
unsigned uTimeResolution;
unsigned nTimeId;
int readPort;
parts of .cpp file
extern "C" void CALLBACK TestTimeProc(UINT id, UINT msg, DWORD
dwUser, DWORD dw1, DWORD dw2 )
{
}
void InitMMTimer()
{
}
void SetMMTimer(UINT microSecond)
{
}
void KillMMTimer()
{
}
void NotifyDummyReadSocket()
{
}
void SelectThreadProc()
{
}