原文网址:http://blog.csdn.net/hongszh/article/details/8608781

最强大的定时器接口来自POSIX时钟系列,其创建、初始化以及删除一个定时器的行动被分为三个不同的函数:timer_create()(创建定时器)、timer_settime()(初始化定时器)以及timer_delete(销毁它)。

man timer_create/timer_settime,可以看到man帮助的详细文档:

[cpp] view plaincopy
  1. TIMER_CREATE(2)                                      Linux Programmer's Manual
  2. NAME
  3. timer_create - create a POSIX per-process timer
  4. SYNOPSIS
  5. #include <signal.h>
  6. #include <time.h>
  7. int timer_create(clockid_t clockid, struct sigevent *sevp,
  8. timer_t *timerid);
  9. int timer_settime(timer_t timerid, int flags,
  10. const struct itimerspec *new_value,
  11. struct itimerspec * old_value);
  12. int timer_gettime(timer_t timerid, struct itimerspec *curr_value);

我的实现如下:

1. 定义timer timeout的函数指针类型:

[cpp] view plaincopy
  1. typedef void (*timerTimeout)(union sigval sig);

2. 为我们的GstPlayer定义两个timer ID:

[cpp] view plaincopy
  1. timer_t  mSeekTimer;
  2. timer_t  mPrepareAsyncTimer;

3. 定义createTimer函数,创建timer,设置timeout函数
    timerId: 输入输出参数
    func:    timer timeout函数

[cpp] view plaincopy
  1. void createTimer(timer_t *timerId, timerTimeout func)
  2. {
  3. struct sigevent sev;
  4. sev.sigev_notify = SIGEV_THREAD;
  5. sev.sigev_signo = SIGRTMIN;
  6. sev.sigev_value.sival_ptr = gPlayer;
  7. sev.sigev_notify_function = func;
  8. sev.sigev_notify_attributes = NULL;
  9. /* create timer */
  10. if (timer_create (CLOCK_REALTIME, &sev, timerId) == -1)
  11. {
  12. ERR ("timer_create, error");
  13. return;
  14. }
  15. if (*timerId == -1)
  16. ERR  ("timer_create error, id is -1");
  17. return;
  18. }

4. setTimer函数, 调用linux的timer_settime, 如果还没到time out,重置之前的timer

如果已经time out,那就得重新调用createTimer生成有效的timer ID,然后才能调用setTimer开始定时器计时。

-这里,将interval参数设置为0,指定我的定时器不工作在循环模式。
-timeMSec是输入参数,指定time out的时间,单位为毫秒。

[cpp] view plaincopy
  1. void setTimer(timer_t *timerId, int timeMSec)
  2. {
  3. struct itimerspec its;
  4. /* Start the timer */
  5. its.it_value.tv_sec = timeMSec / 1000;
  6. its.it_value.tv_nsec = (timeMSec % 1000) * 1000000;
  7. its.it_interval.tv_sec = 0;
  8. its.it_interval.tv_nsec = 0;
  9. if (timer_settime (*timerId, 0, &its, NULL) == -1)
  10. {
  11. ERR ("timer_settime error");
  12. }
  13. DEBUG ("call timer_settime reset timer done.");
  14. return;
  15. }

seekTimerTimeout函数,time out以后,销毁之前调用createTimer创建的timer,完成time out后要做的工作

[cpp] view plaincopy
  1. void seekTimerTimeout(union sigval sig)
  2. {
  3. GstPlayer *player = (GstPlayerplayer*)sig.sival_ptr;
  4. if (player->mSeekTimer != -1)
  5. {
  6. DEBUG("timeout, delete timer:Id = %d",
  7. player->mSeekTimer);
  8. timer_delete(player->mSeekTimer);
  9. player->mSeekTimer = -1;
  10. }
  11. // ... 完成time out后要做的工作
  12. }

prepareAsyncTimeout的time out函数:

[cpp] view plaincopy
  1. void prepareAsyncTimeout(union sigval sig)
  2. {
  3. GstStateChangeReturn state_return;
  4. GstPlayer *player = (GstPlayerplayer*)sig.sival_ptr;
  5. if (player->mPrepareAsyncTimer != -1)
  6. {
  7. DEBUG("timeout, delete timer:Id = %d",
  8. player->mPrepareAsyncTimer);
  9. timer_delete(player->mPrepareAsyncTimer);
  10. player->mPrepareAsyncTimer = -1;
  11. }
  12. // ...完成time out后要做的工作
  13. }

调用一:

[cpp] view plaincopy
  1. 创建timer,设定prepareAsyncTimeout
  2. 开始timer,timeout时间为500ms
  3. createTimer(&mPrepareAsyncTimer, prepareAsyncTimeout);
  4. setTimer(&mPrepareAsyncTimer, 500/*ms*/);

调用二:

[cpp] view plaincopy
  1. 创建timer,设定timeout回调函数。
  2. // create timer
  3. if (mSeekTimer == -1)
  4. {
  5. createTimer(&mSeekTimer, seekTimerTimeout);
  6. }
  7. 判断mSeekTimer是否有效,有效,计算到timeout的剩余时间,如果还没到timeout,重置timer,
  8. 开始新的计时。
  9. // if timer exist and not expire, reset timer.
  10. if (mSeekTimer != -1)
  11. {
  12. gulong remaining = 0; //us
  13. struct itimerspec its;
  14. timer_gettime(mSeekTimer, &its);
  15. remaining = its.it_value.tv_sec * 1000000
  16. its.it_value.tv_nsec / 1000;
  17. DEBUG ("-- remaining time = %lu us", remaining);
  18. if ((100/*ms*/ * 1000 - remaining) > 0)
  19. {
  20. setTimer(&mSeekTimer, 100/*ms*/);
  21. DEBUG ("the new seek interval < 100ms, return");
  22. mSeekCount = 1;
  23. return TRUE;
  24. }
  25. }

参考:

http://blog.163.com/zheng_he_xiang/blog/static/18650532620116311020390/
http://blog.csdn.net/leo9150285/article/details/8271910

转载于:https://www.cnblogs.com/wi100sh/p/4281618.html

【转】Linux Posix Timer使用相关推荐

  1. POSIX Timer

    一.特点: 1.使用 POSIX Timer,一个进程可以创建任意多个 Timer. 2.setitmer 计时器时间到达时,可以有多种通知方式,比如信号,或者启动线程. 3.POSIX Timer ...

  2. POSIX Timer API 的使用

    POSIX Timer API 使用 所需头文件 #include <signal.h> #include <time.h> 链接库-lrt API //创建定时器 int t ...

  3. 定时器 POSIX Timer定时器和setitimer定时器

    POSIX 可移植 POSIX:可移植操作系统接口(Portable Operating System Interface of UNIX,缩写为 POSIX . POSIX Timer C API ...

  4. linux posix 线程池_linux多线程--POSIX Threads Programming

    linux多线程自己从接触很久也有不少实践,但总是觉得理解不够深刻,不够系统.借这篇文章试着再次系统学习一下linux多线程编程,理解编程的concept,细致看一下POSIX pthread API ...

  5. linux中timer的作用,linux - linux / timer.h setup_timer()到期功能不起作用? - 堆栈内存溢出...

    因此,我的setup_timer()中的TimerExpire函数会引起巨大的恐慌(将在下面发布),而对TimerExpire()的常规函数​​调用实际上将输出我的输入. void TimerExpi ...

  6. linux posix 线程池_posix多线程有感--自旋锁

    转自:http://www.csdn123.com/html/blogs/20130509/11141.htm 自旋锁是SMP架构中的一种low-level的同步机制. 当线程A想要获取一把自旋锁而该 ...

  7. windows event与linux,相当于Win32的CreateEvent,SetEvent,WaitForSingleObject的Linux / POSIX

    我写了一个小类来同步Linux(实际上是Android)和Windows的线程. 这是我的界面的Win32实现: class SyncObjectWin32 : public SyncObject { ...

  8. linux POSIX 信号集,读书笔记:第10章 Posix信号量 (6)

    <UNIX网络编程:卷2>P203-P204:图10-33.10-34 生产者和消费者之间使用多个缓冲区 ----------------------------------------- ...

  9. linux进程timer,Linux Timer 示例

    定时器在日常编程中被频繁用到, 很多上层库都提供了很简单的使用方法, 如 glibc 中 g_timeout_add_full 函数. 但只使用 libc 时, 实现起来还是略麻烦的, 所以在此记录下 ...

最新文章

  1. [Sdoi2017]硬币游戏 [高斯消元 KMP]
  2. linux内核计算代码时间,完成一个简单的时间片轮转多道程序内核代码
  3. 黑白树(牛客网+树形dp)
  4. Eclipse使用总结【更新中】
  5. 建立了索引怎么使用_对MySQL索引的认识
  6. 算法工程师面试备战笔记4_余弦相似与欧氏距离有什么区别和联系
  7. Android 使用GridView以表格的形式显示多张图片
  8. 解决Mac osx AirPort: Link Down on en1. Reason 8 (Disassociated because station leaving)
  9. windows 快捷方式(.lnk)代码执行漏洞(CVE-2017-8464 )[附EXP生成工具]
  10. 手机上最好用的五笔输入法_手机输入法哪家最好用?我推荐百度,不好用你打我...
  11. 简谈libxml2库函数的使用
  12. 心理测试软件需求分析报告,大学生心理测试软件心理测评档案管理系统
  13. 蓝牙键盘常用快捷键记录
  14. 各大主流社交软件显示ip地址-如何实现ip飘移
  15. 学习大数据需要哪些数学知识?
  16. VS报错:没有足够的内存继续执行程序
  17. PCL点云处理与关键点提取
  18. VTM1.0代码阅读:CU、PU、TU
  19. 30000台苹果电脑遭恶意软件入侵,包括最新的M1系列!快检查一下自己的电脑
  20. 迅雷链同构多链框架解析

热门文章

  1. 【物联网】 Ubuntu中生成交叉编译器xtensa-lx106-elf
  2. 在线识图翻译_拍照翻译ios版下载在线识图翻译-免费拍照翻译软件苹果版下载...
  3. python线性输出_python sklearn-02:线性回归简单例子1
  4. 关于 jsp java servlet 中文汉字乱码的解决方法
  5. 每天一道LeetCode-----找出给定序列的所有子序列
  6. scanf的一些小细节
  7. 自我分析colly的robots源码
  8. idea-jvm参数设置(有注释)
  9. 第四章 PX4-Pixhawk-MPU6000传感器驱动解析
  10. muduo学习笔记 - 第4章 C++多线程系统编程精要