POCO C++库学习和分析 -- 线程 (三)
4. 定时器
定时器作为线程的扩展,也是编程时经常会被用到的元素。在程序设计上,定时器的作用是很简单。预定某个定时器,即希望在未来的某个时刻,程序能够得到时间到达的触发信号。
编程时,一般对定时器使用有下面一些关注点:
1. 定时器的精度。Poco中的定时器精度并不是很高,具体精度依赖于实现的平台(Windows or Linux)
2. 定时器是否可重复,即定时器是否可触发多次。 Poco中的定时器精度支持多次触发也支持一次触发,由其构造函数Timer决定
Timer(long startInterval = 0, long periodicInterval = 0);
/// Creates a new timer object. StartInterval and periodicInterval
/// are given in milliseconds. If a periodicInterval of zero is
/// specified, the callback will only be called once, after the
/// startInterval expires.
/// To start the timer, call the Start() method.
3. 一个定时器是否可以设置多个时间。 Poco中定时器不支持设置多个时间,每个定时器对应一个时间。如果需要多个时间约定的话,使用者要构造多个定时器。
4.1 定时器实现
Poco中的定时器并不复杂,下面是它的类图。