-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpool_status.cpp
More file actions
35 lines (31 loc) · 1.66 KB
/
Copy pathpool_status.cpp
File metadata and controls
35 lines (31 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <thread_pool/thread_pool.h>
#include <iostream>
int main()
{
abin::threadpool pool(4); // 包含4个线程的线程池
// 查询单个状态
std::cout << "before pool.is_running() = " << pool.is_running(); // 线程池是否在运行
std::cout << "\nbefore pool.total_threads() = " << pool.total_threads(); // 线程池总共的线程数
std::cout << "\nbefore pool.busy_threads() = " << pool.busy_threads(); // 繁忙的线程数量
std::cout << "\nbefore pool.idle_threads() = " << pool.idle_threads(); // 空闲的线程数量
std::cout << "\nbefore pool.pending_tasks() = " << pool.pending_tasks(); // 正在等待的任务数量
for (size_t i = 0; i < 100; ++i) // 提交100个task
{
pool.submit([i] {
std::this_thread::sleep_for(std::chrono::milliseconds(100)); // 模拟任务
return i;
});
}
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "\n===All tasks submitted.===\n";
// 通过status函数获取当前所有的状态信息
abin::threadpool::status_info ss = pool.status();
std::cout << "after status_info.running = " << ss.running; // 线程池是否在运行
std::cout << "\nafter status_info.total_threads = " << ss.total_threads; // 线程池总共的线程数
std::cout << "\nafter status_info.busy_threads = " << ss.busy_threads; // 繁忙的线程数量
std::cout << "\nafter status_info.idle_threads = " << ss.idle_threads; // 空闲的线程数量
std::cout << "\nafter status_info.pending_tasks = " << ss.pending_tasks; // 正在等待的任务数量
std::cout << '\n';
pool.wait_all();
std::cout << "===All tasks completed.===\n";
}