example_tc_mem_queue.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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_mem_queue.h"
  17. #include "util/tc_sem_mutex.h"
  18. #include "util/tc_shm.h"
  19. #include "util/tc_thread_pool.h"
  20. #include "util/tc_common.h"
  21. #include <sstream>
  22. #include <iostream>
  23. using namespace tars;
  24. TC_Shm shm;
  25. TC_SemMutex semLock;
  26. TC_MemQueue memQueue;
  27. /**
  28. * 线程调用过程
  29. * @param s
  30. * @param i
  31. */
  32. void writeQueue()
  33. {
  34. int i = 100000000;
  35. while(i)
  36. {
  37. TC_LockT<TC_SemMutex> l(semLock);
  38. if(memQueue.push_back(TC_Common::tostr(i)))
  39. {
  40. cout << std::this_thread::get_id() << " | writeQueue OK " << i << ":" << memQueue.elementCount() << endl;
  41. i--;
  42. }
  43. else
  44. {
  45. cout << std::this_thread::get_id() << " | writeQueue FULL " << i << endl;
  46. }
  47. }
  48. }
  49. void readQueue()
  50. {
  51. while(true)
  52. {
  53. string s;
  54. TC_LockT<TC_SemMutex> l(semLock);
  55. if(memQueue.pop_front(s))
  56. {
  57. cout << std::this_thread::get_id() << " | readQueue OK " << s << endl;
  58. }
  59. else
  60. {
  61. cout << std::this_thread::get_id() << " | readQueue EMPTY" << endl;
  62. sleep(1);
  63. }
  64. }
  65. }
  66. int main(int argc, char *argv[])
  67. {
  68. try
  69. {
  70. size_t l = 1024000;
  71. shm.init(l, 8000);
  72. semLock.init(8000);
  73. if(shm.iscreate())
  74. {
  75. memQueue.create(shm.getPointer(), l);
  76. }
  77. else
  78. {
  79. memQueue.connect(shm.getPointer(), l);
  80. }
  81. if(argc > 1)
  82. {
  83. TC_ThreadPool twpool;
  84. twpool.init(4);
  85. twpool.start();
  86. for(size_t i = 0; i < twpool.getThreadNum(); i++)
  87. {
  88. twpool.exec(writeQueue);
  89. }
  90. twpool.waitForAllDone();
  91. }
  92. else
  93. {
  94. TC_ThreadPool trpool;
  95. trpool.init(4);
  96. trpool.start();
  97. for(size_t i = 0; i < trpool.getThreadNum(); i++)
  98. {
  99. trpool.exec(readQueue);
  100. }
  101. trpool.waitForAllDone();
  102. }
  103. }
  104. catch(exception &ex)
  105. {
  106. cout << ex.what() << endl;
  107. }
  108. return 0;
  109. }