Chinaunix首页 | 论坛 | 博客
  • 博客访问: 474542
  • 博文数量: 280
  • 博客积分: 337
  • 博客等级: 二等列兵
  • 技术积分: 1957
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-17 21:36
文章分类

全部博文(280)

文章存档

2017年(13)

2016年(38)

2015年(78)

2014年(67)

2013年(70)

2012年(14)

分类: C/C++

2015-02-09 19:53:34


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6. #include <signal.h>
  7. #include <list>
  8.  
  9. pthread_t test_timer_tid;

  10. static long curr_time;
  11. static long min_time = 0;

  12. typedef struct
  13. {
  14.     int start_time; //should be time_t for precision
  15.     int duration; //should be time_t for precision
  16.     void *sock;
  17. }Timer;

  18. std::list<Timer> timers;

  19. static void handler(int s)
  20. {
  21.     //should lock here

  22.     if(timers.empty())
  23.     {
  24.         return;
  25.     }

  26.     std::list<Timer>::iterator it = timers.begin();

  27.     curr_time = time(0);
  28.     min_time = it->duration - (curr_time - it->start_time);
  29.     printf("min_time: %d curr_time: %d\n", (int)min_time, (int)curr_time);
  30.     
  31.     if(min_time > 0)
  32.     {
  33.         alarm(min_time);
  34.     }

  35.     //should unlock here
  36.     
  37.     signal(SIGALRM, handler);
  38. }

  39. static void check_timers()
  40. {
  41.     //should lock here

  42.     std::list<Timer>::iterator it;
  43.     
  44.     curr_time = time(0);
  45.     int i(0), time_left(0);
  46.     for(it = timers.begin(); it != timers.end(); )
  47.     {
  48.         time_left = it->duration - (curr_time - it->start_time);
  49.         printf("----- %d time_left: %d\n", ++i, time_left);
  50.         if(time_left <= 0)
  51.         {
  52.             //it->sock callback, 处理该定时器事件
  53.             timers.erase(it++);
  54.         }
  55.         else
  56.         {
  57.             it++;
  58.         }
  59.     }

  60.     //should unlock here
  61.   
  62.     handler(0);
  63. }

  64. //timer loop, only should be called in main thread
  65. static void timer()
  66. {
  67.     static int count = 1;
  68.     handler(0);
  69.     while(1)
  70.     {
  71.         printf("pause()\n");
  72.         pause();
  73.         printf("count: %d\n", count++);

  74.         //check timer list/queue
  75.         check_timers();
  76.     }
  77.     return;
  78. }

  79. //add a timer
  80. void add_timer(Timer &t)
  81. {
  82.     //should lock here

  83.     std::list<Timer>::iterator it;

  84.     curr_time = time(0);
  85.     for(it = timers.begin(); it != timers.end(); it++)
  86.     {
  87.         if((it->duration - (curr_time - it->start_time)) > t.duration)
  88.         {
  89.             timers.insert(it, t);
  90.             break;
  91.         }
  92.     }
  93.     if(it == timers.end())
  94.     {
  95.         timers.push_back(t);
  96.     }

  97.     //should unlock here
  98.     handler(0);
  99. }

  100. void *test_timer(void *arg)
  101. {
  102.     while(1)
  103.     {
  104.         sleep(2);
  105.         Timer t;
  106.         t.start_time = time(0);
  107.         t.duration = 10;
  108.         add_timer(t);

  109.         if(min_time == 2)
  110.         {
  111.             break;
  112.         }
  113.     }
  114.     return (void*)0;
  115. }

  116. int main()
  117. {
  118.     int err;
  119.     
  120.     //thread for test
  121.     err = pthread_create(&test_timer_tid, NULL, test_timer, NULL);
  122.     if(err != 0)
  123.     {
  124.         printf("create test_timer thread error: %s/n",strerror(err));
  125.         return 1;
  126.     }
  127.     
  128.     timer();

  129.     pthread_join(test_timer_tid, 0);
  130.       
  131.     return 0;
  132. }

阅读(694) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~