Re: 请教大家一下, server中的定时机制(timer)该怎么做 ?

81 views
Skip to first unread message
Message has been deleted

sunway

unread,
May 11, 2006, 10:24:33 PM5/11/06
to 高性能网络编程邮件列表
ACE的做法是可取的。
自己模拟Timer只要控制的好不会影响性能。
很多系统定时器都有限制,比如多媒体定时器,限制为最大64个好像。
有空可以研究下linux下的系统定时器。

Lee Alvin

unread,
May 12, 2006, 3:53:32 AM5/12/06
to dev4s...@googlegroups.com
在 06-5-12,zephyr<zephy...@gmail.com> 写道:
> 请教大家一下, server中的定时机制(timer)该怎么做 ?
> 例如一些定时事件的触发:定时发keepalive,
> 检查socket是否很长时间没有数据,或者定时地广播一些消息等等。
>
> 在windows下可以直接用SetTimer,不过感觉比较不爽(要和窗口关联),
> 在无窗口的工作线程中有什么好的方法可以实现定时机制
> ?

Win32可以使用使用定时器内核对象,CreateWaitableTimer,不和窗口循环关联。
请详见《Windows核心编程》九章四节。

>
> 在linux下怎么实现定时机制 ?
>
> 以前看过ACE, 好像是内部自己维护一个封装过的timer
> Queue, 然后在线程中运行中检测是否到time-out
> 时间然后回调原先指定的timer
> procedure。这种在用户级别的模拟timer实现是否会严重影响性能
> ?
>
> >
>


--
Enjoy your life! Make things clearly and easier!
Reply Mailto: alvi...@gmail.com
My Blog: http://wolf.bloghome.cn

Lee Alvin

unread,
May 12, 2006, 3:55:51 AM5/12/06
to dev4s...@googlegroups.com
ACE应该也是用内核对象上的事件标志来做的,不可能是轮训,可以参考《ACE.Programmers.Guide》上面详细介绍了,ACE的高性能、高精度的Timer。

christanxw

unread,
May 13, 2006, 4:20:20 AM5/13/06
to 高性能网络编程邮件列表
要实现定时器,无非就是轮询或中断,大多数系统实现的定时器都是轮询,而工业控制等对时间精度要求很高的实时操作系统一般都是用中断实现。

Lee Alvin

unread,
May 14, 2006, 1:30:15 PM5/14/06
to dev4s...@googlegroups.com
2006/5/13, christanxw <chris...@gmail.com>:
> 要实现定时器,无非就是轮询或中断,大多数系统实现的定时器都是轮询,而工业控制等对时间精度要求很高的实时操作系统一般都是用中断实现。
>
操作系统IO事件处理一般就是轮讯、中断、DMA、管道几种方式,操作系统内核态通常都是这样实现的。ACE为用户态的库,Timer框架的事件主要由ACE_Timer_Queue来实现,分为ACE_Async_Timer_Queue_Adapter和ACE_Thread_Timer_Queue_Adapter来实现,内部分别使用ACE_Sig_Handler信号和ACE_Condition的超时来实现的。
Message has been deleted

sunway

unread,
May 15, 2006, 2:03:24 AM5/15/06
to 高性能网络编程邮件列表
win下的定时器有下面几种。
1,消息机制的,也就是你说的。
2,多媒体定时器 winmm.h中。
3,队列定时器,queue timer
4,CreateWaitableTimer定时器。
这几种我都用过,感觉queue timer最方便。
Message has been deleted

sunway

unread,
May 16, 2006, 1:53:55 AM5/16/06
to 高性能网络编程邮件列表
CreateTimerQueueTimer() win32 api

sunway

unread,
May 16, 2006, 1:35:06 AM5/16/06
to 高性能网络编程邮件列表
CreateTimerQueueTimer...win32api,win2k...

Message has been deleted

漂舟

unread,
May 16, 2006, 8:55:30 PM5/16/06
to 高性能网络编程邮件列表
都是高手啊,
偶是新来小菜,跟大家学习的

破裤男人

unread,
May 19, 2006, 4:55:50 AM5/19/06
to 高性能网络编程邮件列表
看看Sleep可以吗?

清风雨

unread,
May 21, 2006, 10:58:18 PM5/21/06
to 高性能网络编程邮件列表
我建议可以看看java实现定时器的java源码,然后可以自己写一个简单易用的(就自己的需求情况)。
Message has been deleted

清风雨

unread,
May 24, 2006, 11:09:19 PM5/24/06
to 高性能网络编程邮件列表
java.sun.com有下载的(实际上java的JDK里面的src.zip——选择安装源码)。

定时器的实现可以有最简单的方式:基于系统高精度时间轮询(采用这个方式的,比较适合游戏系统。我在gameres上有篇简单的思考介绍)。

fyt...@yahoo.com.cn

unread,
Jun 5, 2006, 7:57:24 AM6/5/06
to 高性能网络编程邮件列表
使用POSIX线程中的条件变量实现定时.
pthread_cond_timewait
可以实现纳秒级的超时.维护一个链表,每次插入新的请求的时,对链表进行排列.
用一个线程处理链表(alarm_list)中的定时。
比如:
while (alarm_list == NULL)
{
status = pthread_cond_wait (&alarm_cond, &alarm_mutex);
... ...
}
如果在等待的时候,有新的定时请求,则要取消此次status
= pthread_cond_wait (&alarm_cond, &alarm_mutex);
重新将定时插入链表
Reply all
Reply to author
Forward
0 new messages