当前位置:   article > 正文

timerfd和timer

timerfd和timer

note

timerfd对应的时钟到期后,对应内部数据(uint64)计数加1

timerfd支持read方法,poll方法

code

  1. #include <sys/timerfd.h>
  2. #include <poll.h>
  3. #include <thread>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <errno.h>
  7. #include <unistd.h>
  8. void timerfd_test1(void)
  9. {
  10. class timer {
  11. public:
  12. struct callback {
  13. void* (*fun)(void*);
  14. void* arg;
  15. };
  16. timer() {
  17. this->fd = -1;
  18. this->cb = nullptr;
  19. }
  20. ~timer() {
  21. if (this->fd > 0) {
  22. }
  23. }
  24. int init(long interval) {
  25. int ret = -1;
  26. ret = timerfd_create(CLOCK_REALTIME, 0);
  27. if (ret == -1) {
  28. return -1;
  29. }
  30. this->fd = ret;
  31. this->interval = interval;
  32. return 0;
  33. }
  34. int start(void) {
  35. if (!cb) {
  36. return -1;
  37. }
  38. if (fd < 0) {
  39. return -1;
  40. }
  41. struct itimerspec tim;
  42. struct timespec now;
  43. int ret = -1;
  44. ret = clock_gettime(CLOCK_REALTIME, &now);
  45. if (ret == -1) {
  46. return -1;
  47. }
  48. tim.it_interval.tv_sec = interval;
  49. tim.it_interval.tv_nsec = 0;
  50. tim.it_value.tv_nsec = now.tv_nsec;
  51. tim.it_value.tv_sec = now.tv_sec;
  52. ret = timerfd_settime(this->fd, TFD_TIMER_ABSTIME, &tim, nullptr);
  53. if (ret == -1) {
  54. return -1;
  55. }
  56. this->running = true;
  57. std::thread t(&timer::routine, this, this); // 第一个this因为非静态成员函数做线程,第二个this为参数
  58. t.detach();
  59. return 0;
  60. }
  61. int stop(void) {
  62. running = false;
  63. return 0;
  64. }
  65. int setcb(callback* cbIn) {
  66. if (!cbIn) {
  67. return -1;
  68. }
  69. if (cbIn->fun == nullptr) {
  70. return -1;
  71. }
  72. if (cbIn->arg == nullptr) {
  73. return -1;
  74. }
  75. if (this->cb) {
  76. return -1;
  77. }
  78. this->cb = cbIn;
  79. return 0;
  80. }
  81. protected:
  82. private:
  83. int fd;
  84. long interval;
  85. bool running;
  86. callback* cb;
  87. void routine(void* arg) {
  88. if (arg == nullptr) {
  89. return;
  90. }
  91. timer* me = static_cast<timer*>(arg);
  92. int ret = -1;
  93. struct pollfd fds;
  94. uint64_t cnt = 0;
  95. ssize_t n = 0;
  96. fds.fd = me->fd;
  97. fds.events = POLLIN;
  98. fds.revents = 0;
  99. while (me->running) {
  100. ret = poll(&fds, 1, 0);
  101. if (ret == -1) {
  102. fprintf(stderr, "poll error,%s\n", strerror(errno));
  103. continue;
  104. }
  105. if (ret == 0) {
  106. // fprintf(stdout, "poll return no event\n");
  107. continue;
  108. }
  109. if ((ret == 1) && (fds.revents == POLLIN)) {
  110. // fprintf(stdout, "poll return read event\n");
  111. n = read(fds.fd, &cnt, sizeof(uint64_t));
  112. if (n < 0) {
  113. fprintf(stderr, "read error,%s\n", strerror(errno));
  114. continue;
  115. }
  116. if (n != sizeof(uint64_t)) {
  117. fprintf(stdout, "read return not uint64_t\n");
  118. continue;
  119. }
  120. for (uint64_t i = 0; i < cnt; ++i) {
  121. (me->cb->fun)(me->cb->arg);
  122. }
  123. }
  124. }
  125. }
  126. };
  127. timer t;
  128. timer::callback cb;
  129. auto f = [](void*arg) -> void* {
  130. fprintf(stdout, "%s calleb,arg:%s\n", __FUNCTION__, (char*)arg);
  131. return nullptr;
  132. };
  133. cb.fun = f;
  134. cb.arg = (void*)"hello";
  135. t.setcb(&cb);
  136. t.init(1);
  137. t.start();
  138. std::this_thread::sleep_for(std::chrono::milliseconds(10000));
  139. t.stop();
  140. std::this_thread::sleep_for(std::chrono::milliseconds(10000));
  141. fprintf(stdout, "%s returning...\n", __FUNCTION__);
  142. }

test

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/471004
推荐阅读
相关标签
  

闽ICP备14008679号