关键词搜索

源码搜索 ×
×

tars源码分析之15

发布2022-07-10浏览588次

详情内容

条件变量,看过unix高级边境编程的都知道啦:

  1. #include "util/tc_thread_cond.h"
  2. #include "util/tc_timeprovider.h"
  3. #include <string.h>
  4. #include <cassert>
  5. #include <iostream>
  6. using namespace std;
  7. namespace tars
  8. {
  9. TC_ThreadCond::TC_ThreadCond()
  10. {
  11. int rc;
  12. pthread_condattr_t attr;
  13. rc = pthread_condattr_init(&attr);
  14. if(rc != 0)
  15. {
  16. throw TC_ThreadCond_Exception("[TC_ThreadCond::TC_ThreadCond] pthread_condattr_init error", errno);
  17. }
  18. rc = pthread_cond_init(&_cond, &attr);
  19. if(rc != 0)
  20. {
  21. throw TC_ThreadCond_Exception("[TC_ThreadCond::TC_ThreadCond] pthread_cond_init error", errno);
  22. }
  23. rc = pthread_condattr_destroy(&attr);
  24. if(rc != 0)
  25. {
  26. throw TC_ThreadCond_Exception("[TC_ThreadCond::TC_ThreadCond] pthread_condattr_destroy error", errno);
  27. }
  28. }
  29. TC_ThreadCond::~TC_ThreadCond()
  30. {
  31. int rc = 0;
  32. rc = pthread_cond_destroy(&_cond);
  33. if(rc != 0)
  34. {
  35. cerr << "[TC_ThreadCond::~TC_ThreadCond] pthread_cond_destroy error:" << string(strerror(rc)) << endl;
  36. }
  37. // assert(rc == 0);
  38. }
  39. void TC_ThreadCond::signal()
  40. {
  41. int rc = pthread_cond_signal(&_cond);
  42. if(rc != 0)
  43. {
  44. throw TC_ThreadCond_Exception("[TC_ThreadCond::signal] pthread_cond_signal error", errno);
  45. }
  46. }
  47. void TC_ThreadCond::broadcast()
  48. {
  49. int rc = pthread_cond_broadcast(&_cond);
  50. if(rc != 0)
  51. {
  52. throw TC_ThreadCond_Exception("[TC_ThreadCond::broadcast] pthread_cond_broadcast error", errno);
  53. }
  54. }
  55. timespec TC_ThreadCond::abstime( int millsecond) const
  56. {
  57. struct timeval tv;
  58. //gettimeofday(&tv, 0);
  59. TC_TimeProvider::getInstance()->getNow(&tv);
  60. int64_t it = tv.tv_sec * (int64_t)1000000 + tv.tv_usec + (int64_t)millsecond * 1000;
  61. tv.tv_sec = it / (int64_t)1000000;
  62. tv.tv_usec = it % (int64_t)1000000;
  63. timespec ts;
  64. ts.tv_sec = tv.tv_sec;
  65. ts.tv_nsec = tv.tv_usec * 1000;
  66. return ts;
  67. }
  68. }

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载