example_tc_thread_queue.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #include "util/tc_monitor.h"
  17. #include "util/tc_common.h"
  18. #include "util/tc_thread_queue.h"
  19. #include "util/tc_autoptr.h"
  20. #include "util/tc_thread.h"
  21. #include "util/tc_logger.h"
  22. #include <iostream>
  23. using namespace tars;
  24. TC_ThreadQueue<string> _queue;
  25. TC_RollLogger _logger;
  26. struct A : public TC_HandleBase
  27. {
  28. };
  29. typedef TC_AutoPtr<A> APtr;
  30. class WriteThread : public TC_Thread, public TC_HandleBase
  31. {
  32. /**
  33. * 运行
  34. */
  35. protected:
  36. virtual void run()
  37. {
  38. while(true)
  39. {
  40. timeval t1;
  41. gettimeofday(&t1, NULL);
  42. _queue.push_back("abc");
  43. usleep(1000);
  44. timeval t2;
  45. gettimeofday(&t2, NULL);
  46. _logger.debug() << "push_back:" << t2.tv_usec - t1.tv_usec << endl;
  47. usleep(1000);
  48. cout << "write" << endl;
  49. }
  50. }
  51. };
  52. typedef TC_AutoPtr<WriteThread> WriteThreadPtr;
  53. class ReadThread : public TC_Thread, public TC_HandleBase
  54. {
  55. /**
  56. * 运行
  57. */
  58. protected:
  59. virtual void run()
  60. {
  61. while(true)
  62. {
  63. string t;
  64. timeval t1;
  65. gettimeofday(&t1, NULL);
  66. if(_queue.pop_front(t, 60000))
  67. {
  68. timeval t2;
  69. gettimeofday(&t2, NULL);
  70. _logger.debug() << "pop_front:" << t2.tv_usec - t1.tv_usec << endl;
  71. // cout << std::this_thread::get_id() << ":" << t << endl;
  72. usleep(20 * 1000);
  73. }
  74. }
  75. }
  76. };
  77. typedef TC_AutoPtr<ReadThread> ReadThreadPtr;
  78. int main(int argc, char *argv[])
  79. {
  80. try
  81. {
  82. WriteThreadPtr wt = new WriteThread();
  83. wt->start();
  84. vector<ReadThreadPtr> rts;
  85. for(size_t i = 0; i < 3;i++)
  86. {
  87. rts.push_back(new ReadThread());
  88. rts.back()->start();
  89. }
  90. wt->getThreadControl().join();
  91. }
  92. catch(exception &ex)
  93. {
  94. cout << ex.what() << endl;
  95. }
  96. return 0;
  97. }