main.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // #include <unistd.h>
  2. #include <iostream>
  3. #include <memory>
  4. #include <thread>
  5. #include <mutex>
  6. #include <queue>
  7. #include <condition_variable>
  8. #include <functional>
  9. #include <future>
  10. #include <numeric>
  11. #include <cmath>
  12. #include <list>
  13. #include <iterator>
  14. #include "ThreadPool.h"
  15. #include "loginit.h"
  16. #include "spdlog/spdlog.h"
  17. /* 模拟子线程 */
  18. void print(int a)
  19. {
  20. SPDLOG_DEBUG("这是一个子线程:{}", a);
  21. std::this_thread::sleep_for(std::chrono::seconds(a + 1));
  22. SPDLOG_DEBUG("子线程:{}运行结束", a);
  23. }
  24. int ret_print(int a)
  25. {
  26. // SPDLOG_DEBUG("这是一个子线程:{}", a);
  27. std::this_thread::sleep_for(std::chrono::seconds(a + 1));
  28. SPDLOG_DEBUG("子线程:{}运行结束", a);
  29. return a;
  30. }
  31. int main(int argc, char *argv[])
  32. {
  33. using namespace std;
  34. init_log();
  35. auto logger = spdlog::get("main");
  36. SPDLOG_LOGGER_INFO(logger,"********** Hello ThreadPool **********");
  37. ThreadPool &tp = ThreadPool::getInstance();
  38. SPDLOG_LOGGER_DEBUG(logger,"线程池最大线程个数:{}", tp.getThreadMaxNum());
  39. SPDLOG_LOGGER_DEBUG(logger,"线程池最小线程个数:{}", tp.getThreadMiniNum());
  40. SPDLOG_LOGGER_DEBUG(logger,"线程池空闲线程的个数:{}", tp.getThreadIdleNum());
  41. SPDLOG_LOGGER_DEBUG(logger,"线程池正在运行的线程个数:{}", tp.getThreadRunNum());
  42. SPDLOG_LOGGER_DEBUG(logger,"线程池现存的线程个数:{}", tp.getThreadLiveNum());
  43. SPDLOG_LOGGER_DEBUG(logger,"线程池每次创建线程的个数:{}", tp.getThreadAddNum());
  44. SPDLOG_LOGGER_DEBUG(logger,"线程池最小空闲线程的个数:{}", tp.getThreadMiniIdle());
  45. SPDLOG_LOGGER_DEBUG(logger,"线程池最大空闲线程的个数:{}", tp.getThreadMaxIdle());
  46. SPDLOG_LOGGER_DEBUG(logger,"***** 开始给线程添加任务 ******\n");
  47. // for (int i = 0; i < 20; i++)
  48. // {
  49. // tp.add_Task(print, i);
  50. // std::this_thread::sleep_for(std::chrono::microseconds(100));
  51. // }
  52. std::vector<std::future<int>> vec;
  53. for(int i = 0; i < 20; i++)
  54. {
  55. vec.emplace_back(tp.add_task_with_ret(ret_print, i));
  56. }
  57. for(auto& it : vec)
  58. {
  59. SPDLOG_LOGGER_DEBUG(logger,"返回值:{}", it.get());
  60. }
  61. while(getchar() != 'q')
  62. {
  63. SPDLOG_LOGGER_DEBUG(logger,"=================== 线程池状态 ===================");
  64. SPDLOG_LOGGER_DEBUG(logger,"线程池最大线程个数:{}", tp.getThreadMaxNum());
  65. SPDLOG_LOGGER_DEBUG(logger,"线程池最小线程个数:{}", tp.getThreadMiniNum());
  66. SPDLOG_LOGGER_DEBUG(logger,"线程池空闲线程的个数:{}", tp.getThreadIdleNum());
  67. SPDLOG_LOGGER_DEBUG(logger,"线程池正在运行的线程个数:{}", tp.getThreadRunNum());
  68. SPDLOG_LOGGER_DEBUG(logger,"线程池现存的线程个数:{}", tp.getThreadLiveNum());
  69. SPDLOG_LOGGER_DEBUG(logger,"线程池每次创建线程的个数:{}", tp.getThreadAddNum());
  70. SPDLOG_LOGGER_DEBUG(logger,"线程池最小空闲线程的个数:{}", tp.getThreadMiniIdle());
  71. SPDLOG_LOGGER_DEBUG(logger,"线程池最大空闲线程的个数:{}", tp.getThreadMaxIdle());
  72. std::this_thread::sleep_for(std::chrono::seconds(1));
  73. }
  74. // while(true)
  75. // {
  76. // std::this_thread::sleep_for(std::chrono::seconds(5));
  77. // }
  78. return 0;
  79. }