example_tc_thread_pool.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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_thread_pool.h"
  17. #include "util/tc_common.h"
  18. #include <iostream>
  19. #include <vector>
  20. using namespace std;
  21. using namespace tars;
  22. TC_ThreadPool tpool;
  23. TC_ThreadLock l;
  24. /**
  25. * 线程私有数据
  26. */
  27. class MyThreadData : public TC_ThreadPool::ThreadData
  28. {
  29. public:
  30. virtual ~MyThreadData()
  31. {
  32. cout << std::this_thread::get_id() << endl;
  33. }
  34. public:
  35. pthread_t _idata;
  36. };
  37. /**
  38. * 线程初始化
  39. */
  40. void threadInitialize()
  41. {
  42. MyThreadData *p = TC_ThreadPool::ThreadData::makeThreadData<MyThreadData>();
  43. p->_idata = std::this_thread::get_id();
  44. TC_ThreadPool::setThreadData(p);
  45. cout << p->_idata << endl;
  46. }
  47. /**
  48. * 线程调用过程
  49. * @param s
  50. * @param i
  51. */
  52. void TestFunction3(const string &s, int i)
  53. {
  54. MyThreadData *p = (MyThreadData*)TC_ThreadPool::getThreadData();
  55. assert(std::this_thread::get_id() == p->_idata);
  56. // cout << std::this_thread::get_id() << " | TestFunction3('" << s << "', " << i << ")" << endl;
  57. }
  58. /**
  59. * 运行线程池
  60. */
  61. void testThreadPool()
  62. {
  63. //4个线程
  64. tpool.init(4);
  65. //启动线程, 指定初始化对象,也可以没有初始化对象:tpool.start();
  66. tpool.start(threadInitialize);
  67. string s("a");
  68. int i = 1000000;
  69. //调用i次
  70. while(i)
  71. {
  72. tpool.exec(std::bind(&TestFunction3, std::cref(s), i));
  73. --i;
  74. }
  75. //等待线程结束
  76. cout << "waitForAllDone..." << endl;
  77. bool b = tpool.waitForAllDone(1000);
  78. cout << "waitForAllDone..." << b << ":" << tpool.getJobNum() << endl;
  79. //停止线程,析够的时候也会自动停止线程
  80. //线程结束时,会自动释放私有数据
  81. tpool.stop();
  82. }
  83. void test(int i, string &s)
  84. {
  85. cout << i << ":" << s << endl;
  86. s = TC_Common::tostr(i + 10);
  87. }
  88. /**
  89. * 运行线程池
  90. */
  91. void testThreadPool1()
  92. {
  93. //4个线程
  94. tpool.init(1);
  95. //启动线程, 指定初始化对象,也可以没有初始化对象:tpool.start();
  96. tpool.start();
  97. string bid;
  98. for(int i=0; i<10; i++)
  99. {
  100. bid = TC_Common::tostr(i);
  101. cout << bid << endl;
  102. cout << "index = " << i << ",bid = " << bid << endl;
  103. tpool.exec(std::bind(&test, i, std::ref(bid)));
  104. }
  105. //等待线程结束
  106. cout << "waitForAllDone..." << endl;
  107. bool b = tpool.waitForAllDone(1000);
  108. cout << "waitForAllDone..." << b << ":" << tpool.getJobNum() << endl;
  109. //停止线程,析够的时候也会自动停止线程
  110. //线程结束时,会自动释放私有数据
  111. tpool.stop();
  112. }
  113. int main(int argc, char *argv[])
  114. {
  115. try
  116. {
  117. testThreadPool1();
  118. }
  119. catch(exception &ex)
  120. {
  121. cout << ex.what() << endl;
  122. }
  123. return 0;
  124. }