Linux下的定时器类实现(select定时+线程)

本文介绍了一种在LINUX环境下利用线程和select实现的精确定时器类,该定时器可以有效避免与sleep函数冲突的问题,并支持创建多个独立运行的定时器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文在这里:http://blog.youkuaiyun.com/hejianhua/article/details/6249056

更好的计时器类实现:LINUX RTC机制实现计时器类

      很多时候需要在LINUX下用到定时器,但像setitimer()和alarm()这样的定时器有时会和sleep()函数发生冲突,这样就给编程带来了很大的困难。
    写了一个定时器的类,使用select进行精确定时。而且可以在系统中创建不限数量的定时器,且互不干扰。类的内部采用线程实现。即线程+select。代码如下:

[cpp]  view plain copy
  1. CTimer.h:  
  2. /* 
  3. * CTimer.h 
  4. * 
  5. * Created on: 2009-7-13 
  6. *      Author: DEAN 
  7. */  
  8. //////////////////////////////////////////////////////////////////////////  
  9. // This class provide a timer to finish some works.  
  10. // Call SetTimer() to set the timer_interval. Call StartTimer()  
  11. // to enable it and call StopTimer() to stop it.  
  12. // The work you want to do should be written on OnTimer  
  13. // function.  
  14. //////////////////////////////////////////////////////////////////////////  
  15. #ifndef CTIMER_H_  
  16. #define CTIMER_H_  
  17. #include <pthread.h>  
  18. #include <sys/time.h>  
  19. class CTimer  
  20. {  
  21. private:  
  22.     pthread_t thread_timer;  
  23.     long m_second, m_microsecond;  
  24.     static void *OnTimer_stub(void *p)  
  25.     {  
  26.         (static_cast<CTimer*>(p))->thread_proc();  
  27.     }  
  28.     void thread_proc();  
  29.     void OnTimer();  
  30. public:  
  31.     CTimer();  
  32.     CTimer(long second, long microsecond);  
  33.     virtual ~CTimer();  
  34.     void SetTimer(long second,long microsecond);  
  35.     void StartTimer();  
  36.     void StopTimer();  
  37. };  
  38. #endif /* CTIMER_H_ */  
  39. CTimer.cpp:  
  40. /* 
  41. * CTimer.cpp 
  42. * 
  43. * Created on: 2009-7-13 
  44. *      Author: DEAN 
  45. */  
  46. #include "CTimer.h"  
  47. #include <iostream>  
  48. #include <sys/select.h>  
  49. #include <time.h>  
  50. #include <pthread.h>  
  51. using namespace std;  
  52. //////////////////////////public methods//////////////////////////  
  53. CTimer::CTimer():  
  54.     m_second(0), m_microsecond(0)  
  55. {  
  56. }  
  57. CTimer::CTimer(long second, long microsecond) :  
  58.     m_second(second), m_microsecond(microsecond)  
  59. {  
  60. }  
  61. CTimer::~CTimer()  
  62. {  
  63. }  
  64. void CTimer::SetTimer(long second, long microsecond)  
  65. {  
  66.     m_second = second;  
  67.     m_microsecond = microsecond;  
  68. }  
  69. void CTimer::StartTimer()  
  70. {  
  71.     pthread_create(&thread_timer, NULL, OnTimer_stub, this);  
  72. }  
  73. void CTimer::StopTimer()  
  74. {  
  75.     pthread_cancel(thread_timer);  
  76.     pthread_join(thread_timer, NULL); //wait the thread stopped  
  77. }  
  78. //////////////////////////private methods//////////////////////////  
  79. void CTimer::thread_proc()  
  80. {  
  81.     while (true)  
  82.     {  
  83.         OnTimer();  
  84.         pthread_testcancel();  
  85.         struct timeval tempval;  
  86.         tempval.tv_sec = m_second;  
  87.         tempval.tv_usec = m_microsecond;  
  88.         select(0, NULL, NULL, NULL, &tempval);  
  89.     }  
  90. }  
  91. void CTimer::OnTimer()  
  92. {  
  93.     cout<<"Timer once..."<<endl;  
  94. }  
  95. 示例代码main.cpp:  
  96. /* 
  97. * main.cpp 
  98. * 
  99. * Created on: 2009-7-19 
  100. *      Author: DEAN 
  101. */  
  102. #include <iostream>  
  103. #include "CTimer.h"  
  104. using namespace std;  
  105. int main()  
  106. {  
  107.     CTimer t1(1,0),t2(1,0);    //构造函数,设两个定时器,以1秒为触发时间。参数1是秒,参数2是微秒。  
  108.     t1.StartTimer();  
  109.     t2.StartTimer();  
  110.     sleep(10);  
  111.     return 0;  
  112. }  
 

    使用的话其实很简单,只要写一下OnTimer()函数的内容就行了,定时器会在每个定时器触发时调用此函数。里面用到的一个点是使用类的成员函数作为线程体的执行函数,需要进行一下静态类型转换。在上面已标出:
    static void *OnTimer_stub(void *p)
    {
        (static_cast<CTimer*>(p))->thread_proc();
    }
    
有了这个类以后,使用定时器就方便多了:-)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值