main.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/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. CPPTP.add_task([&](){
  48. while (true)
  49. {
  50. SPDLOG_LOGGER_INFO(logger,"这是一个无参数的任务");
  51. std::this_thread::sleep_for(std::chrono::seconds(1));
  52. }
  53. });
  54. // for (int i = 0; i < 20; i++)
  55. // {
  56. // tp.add_Task(print, i);
  57. // std::this_thread::sleep_for(std::chrono::microseconds(100));
  58. // }
  59. // std::vector<std::future<int>> vec;
  60. // for(int i = 0; i < 20; i++)
  61. // {
  62. // vec.emplace_back(tp.add_task_with_ret(ret_print, i));
  63. // }
  64. // for(auto& it : vec)
  65. // {
  66. // SPDLOG_LOGGER_DEBUG(logger,"返回值:{}", it.get());
  67. // }
  68. while(getchar() != 'q')
  69. {
  70. SPDLOG_LOGGER_DEBUG(logger,"=================== 线程池状态 ===================");
  71. SPDLOG_LOGGER_DEBUG(logger,"线程池最大线程个数:{}", tp.getThreadMaxNum());
  72. SPDLOG_LOGGER_DEBUG(logger,"线程池最小线程个数:{}", tp.getThreadMiniNum());
  73. SPDLOG_LOGGER_DEBUG(logger,"线程池空闲线程的个数:{}", tp.getThreadIdleNum());
  74. SPDLOG_LOGGER_DEBUG(logger,"线程池正在运行的线程个数:{}", tp.getThreadRunNum());
  75. SPDLOG_LOGGER_DEBUG(logger,"线程池现存的线程个数:{}", tp.getThreadLiveNum());
  76. SPDLOG_LOGGER_DEBUG(logger,"线程池每次创建线程的个数:{}", tp.getThreadAddNum());
  77. SPDLOG_LOGGER_DEBUG(logger,"线程池最小空闲线程的个数:{}", tp.getThreadMiniIdle());
  78. SPDLOG_LOGGER_DEBUG(logger,"线程池最大空闲线程的个数:{}", tp.getThreadMaxIdle());
  79. std::this_thread::sleep_for(std::chrono::seconds(1));
  80. }
  81. // while(true)
  82. // {
  83. // std::this_thread::sleep_for(std::chrono::seconds(5));
  84. // }
  85. return 0;
  86. }