123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- // #include <unistd.h>
- #include <iostream>
- #include <memory>
- #include <thread>
- #include <mutex>
- #include <queue>
- #include <condition_variable>
- #include <functional>
- #include <future>
- #include <numeric>
- #include <cmath>
- #include <list>
- #include <iterator>
- #include "ThreadPool/ThreadPool.h"
- #include "loginit.h"
- #include "spdlog/spdlog.h"
- /* 模拟子线程 */
- void print(int a)
- {
- SPDLOG_DEBUG("这是一个子线程:{}", a);
- std::this_thread::sleep_for(std::chrono::seconds(a + 1));
- SPDLOG_DEBUG("子线程:{}运行结束", a);
- }
- int ret_print(int a)
- {
- // SPDLOG_DEBUG("这是一个子线程:{}", a);
- std::this_thread::sleep_for(std::chrono::seconds(a + 1));
- SPDLOG_DEBUG("子线程:{}运行结束", a);
- return a;
- }
- int main(int argc, char *argv[])
- {
- using namespace std;
- init_log();
- auto logger = spdlog::get("main");
- SPDLOG_LOGGER_INFO(logger,"********** Hello ThreadPool **********");
- ThreadPool &tp = ThreadPool::getInstance();
- SPDLOG_LOGGER_DEBUG(logger,"线程池最大线程个数:{}", tp.getThreadMaxNum());
- SPDLOG_LOGGER_DEBUG(logger,"线程池最小线程个数:{}", tp.getThreadMiniNum());
- SPDLOG_LOGGER_DEBUG(logger,"线程池空闲线程的个数:{}", tp.getThreadIdleNum());
- SPDLOG_LOGGER_DEBUG(logger,"线程池正在运行的线程个数:{}", tp.getThreadRunNum());
- SPDLOG_LOGGER_DEBUG(logger,"线程池现存的线程个数:{}", tp.getThreadLiveNum());
- SPDLOG_LOGGER_DEBUG(logger,"线程池每次创建线程的个数:{}", tp.getThreadAddNum());
- SPDLOG_LOGGER_DEBUG(logger,"线程池最小空闲线程的个数:{}", tp.getThreadMiniIdle());
- SPDLOG_LOGGER_DEBUG(logger,"线程池最大空闲线程的个数:{}", tp.getThreadMaxIdle());
- SPDLOG_LOGGER_DEBUG(logger,"***** 开始给线程添加任务 ******\n");
- CPPTP.add_task([&](){
- while (true)
- {
- SPDLOG_LOGGER_INFO(logger,"这是一个无参数的任务");
- std::this_thread::sleep_for(std::chrono::seconds(1));
- }
- });
- // for (int i = 0; i < 20; i++)
- // {
- // tp.add_Task(print, i);
- // std::this_thread::sleep_for(std::chrono::microseconds(100));
- // }
- // std::vector<std::future<int>> vec;
- // for(int i = 0; i < 20; i++)
- // {
- // vec.emplace_back(tp.add_task_with_ret(ret_print, i));
- // }
- // for(auto& it : vec)
- // {
- // SPDLOG_LOGGER_DEBUG(logger,"返回值:{}", it.get());
- // }
- while(getchar() != 'q')
- {
- SPDLOG_LOGGER_DEBUG(logger,"=================== 线程池状态 ===================");
- SPDLOG_LOGGER_DEBUG(logger,"线程池最大线程个数:{}", tp.getThreadMaxNum());
- SPDLOG_LOGGER_DEBUG(logger,"线程池最小线程个数:{}", tp.getThreadMiniNum());
- SPDLOG_LOGGER_DEBUG(logger,"线程池空闲线程的个数:{}", tp.getThreadIdleNum());
- SPDLOG_LOGGER_DEBUG(logger,"线程池正在运行的线程个数:{}", tp.getThreadRunNum());
- SPDLOG_LOGGER_DEBUG(logger,"线程池现存的线程个数:{}", tp.getThreadLiveNum());
- SPDLOG_LOGGER_DEBUG(logger,"线程池每次创建线程的个数:{}", tp.getThreadAddNum());
- SPDLOG_LOGGER_DEBUG(logger,"线程池最小空闲线程的个数:{}", tp.getThreadMiniIdle());
- SPDLOG_LOGGER_DEBUG(logger,"线程池最大空闲线程的个数:{}", tp.getThreadMaxIdle());
- std::this_thread::sleep_for(std::chrono::seconds(1));
- }
- // while(true)
- // {
- // std::this_thread::sleep_for(std::chrono::seconds(5));
- // }
- return 0;
- }
|