POCO C++库学习和分析 -- 线程 (三)
来源:互联网 发布:炫踪网络红杉 编辑:程序博客网 时间:2024/06/05 17:59
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中的定时器并不复杂,下面是它的类图。
在类图中,Timer继承自Runnable类,也就是说Timer实现了自己的run函数。来看一看,run函数的实现。
- void Timer::run()
- {
- Poco::Timestamp now;
- long interval(0);
- do
- {
- long sleep(0);
- do
- {
- now.update();
- sleep = static_cast<long>((_nextInvocation - now)/1000);
- if (sleep < 0)
- {
- if (interval == 0)
- {
- sleep = 0;
- break;
- }
- _nextInvocation += interval*1000;
- ++_skipped;
- }
- }
- while (sleep < 0);
- if (_wakeUp.tryWait(sleep))
- {
- Poco::FastMutex::ScopedLock lock(_mutex);
- _nextInvocation.update();
- interval = _periodicInterval;
- }
- else
- {
- try
- {
- _pCallback->invoke(*this);
- }
- catch (Poco::Exception& exc)
- {
- Poco::ErrorHandler::handle(exc);
- }
- catch (std::exception& exc)
- {
- Poco::ErrorHandler::handle(exc);
- }
- catch (...)
- {
- Poco::ErrorHandler::handle();
- }
- interval = _periodicInterval;
- }
- _nextInvocation += interval*1000;
- _skipped = 0;
- }
- while (interval > 0);
- _done.set();
- }
4.2 定时器使用
最后让我们来看一个定时器的例子:
- #include "Poco/Timer.h"
- #include "Poco/Thread.h"
- using Poco::Timer;
- using Poco::TimerCallback;
- class TimerExample
- {
- public:
- void onTimer(Poco::Timer& timer)
- {
- std::cout << "onTimer called." << std::endl;
- }
- };
- int main(int argc, char** argv)
- {
- TimerExample te;
- Timer timer(250, 500); // fire after 250ms, repeat every 500ms
- timer.start(TimerCallback<TimerExample>(te, &TimerExample::onTimer));
- Thread::sleep(5000);
- timer.stop();
- return 0;
- }
0 0
- POCO C++库学习和分析 -- 线程 (三)
- POCO C++库学习和分析 -- 线程 (三)
- POCO C++库学习和分析 -- 线程 (二)
- POCO C++库学习和分析 -- 线程 (四)
- POCO C++库学习和分析 -- 线程 (一)
- POCO C++库学习和分析 -- 线程 (二)
- POCO C++库学习和分析 -- 流 (三)
- POCO C++库学习和分析 -- 线程 (一)
- POCO C++库学习和分析 -- 内存管理 (三)
- POCO C++库学习和分析 -- 内存管理 (三)
- POCO C++库学习和分析 -- 通知和事件 (三)
- POCO C++库学习和分析 -- 流 (一)
- POCO C++库学习和分析 -- 流 (二)
- POCO C++库学习和分析 -- 序
- POCO C++库学习和分析 -- 任务
- POCO C++库学习和分析 -- 进程
- POCO C++库学习和分析 -- 哈希
- POCO C++库学习和分析 -- Cache
- MySQL 查找重复值
- poj-2253 Frogger(最佳路径 Warshall算法)
- win8/8.1 离线安装 .Net3.5
- 数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历
- USACO Street Race 解题报告
- POCO C++库学习和分析 -- 线程 (三)
- discuz站点迁移或者更换域名后引起的云平台异常解放方法
- 设置布局问题
- 修改Memory_max_target为0出错
- 取得 APP 自己的版本号 (狠跨 4 个平台)
- WIN32字符串转换
- 内部类 外部类区别与联系
- Java中 内存区域划分
- Java中的异常处理机制的简单原理和应用。