Skip to content

Commit 601d70b

Browse files
committed
调整线程池的内容描述,修改提供的测试链接,调整顺序。修改测试的 demo,为线程池增加一些描述解释
1 parent bd6868f commit 601d70b

File tree

1 file changed

+65
-39
lines changed

1 file changed

+65
-39
lines changed

md/详细分析/04线程池.md

Lines changed: 65 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,14 @@ int main() {
104104
boost::asio::thread_pool pool{4}; // 创建一个包含 4 个线程的线程池
105105

106106
for (int i = 0; i < 10; ++i) {
107-
boost::asio::post(pool, [i]() { print_task(i); });
107+
boost::asio::post(pool, [i] { print_task(i); });
108108
}
109109

110110
pool.join(); // 等待所有任务执行完成
111111
}
112112
```
113113
114-
> [运行](https://godbolt.org/z/h5G5hTT4a)测试。
114+
> [运行](https://godbolt.org/z/Pa3z1oYej)测试。
115115
116116
- 创建线程池时,指定线程数量,线程池会创建对应数量的线程。
117117
@@ -297,7 +297,7 @@ class ThreadPool {
297297
- 用于线程间的同步,允许线程等待特定条件(如新任务加入队列)并在条件满足时唤醒线程。
298298

299299
3. **`std::atomic<bool> stop_`**
300-
- 用于指示线程池是否停止接收新任务,并安全地通知所有工作线程退出
300+
- 指示线程池是否停止
301301

302302
4. **`std::atomic<std::size_t> num_threads_`**
303303
- 表示线程池中的线程数量。
@@ -309,7 +309,22 @@ class ThreadPool {
309309

310310
- 线程容器,存储管理线程对象,每个线程从任务队列中获取任务并执行。
311311

312-
那么再直接提供构造函数以及添加任务到线程池的接口,然后内部再进行一点小小的操作,也就完成了:
312+
**标头依赖**
313+
314+
```cpp
315+
#include <iostream>
316+
#include <thread>
317+
#include <mutex>
318+
#include <condition_variable>
319+
#include <future>
320+
#include <atomic>
321+
#include <queue>
322+
#include <vector>
323+
#include <syncstream>
324+
#include <functional>
325+
```
326+
327+
提供构造析构函数,以及一些外部接口:`submit()``start()``stop()``join()`,也就完成了:
313328

314329
```cpp
315330
inline std::size_t default_thread_pool_size()noexcept {
@@ -330,7 +345,7 @@ public:
330345
start();
331346
}
332347

333-
~ThreadPool() {
348+
~ThreadPool(){
334349
stop();
335350
join();
336351
}
@@ -340,7 +355,7 @@ public:
340355
cv_.notify_all();
341356
}
342357

343-
void join() {
358+
void join(){
344359
for (auto& thread : pool_) {
345360
if (thread.joinable()) {
346361
thread.join();
@@ -368,8 +383,8 @@ public:
368383
return ret;
369384
}
370385

371-
void start() {
372-
for (std::size_t i = 0; i < num_threads_; ++i) {
386+
void start(){
387+
for (std::size_t i = 0; i < num_threads_; ++i){
373388
pool_.emplace_back([this] {
374389
while (!stop_) {
375390
Task task;
@@ -383,7 +398,7 @@ public:
383398
}
384399
task();
385400
}
386-
});
401+
});
387402
}
388403
}
389404

@@ -397,74 +412,85 @@ private:
397412
};
398413
```
399414

400-
**标头依赖**
401-
402-
```cpp
403-
#include <iostream>
404-
#include <thread>
405-
#include <mutex>
406-
#include <condition_variable>
407-
#include <future>
408-
#include <atomic>
409-
#include <queue>
410-
#include <vector>
411-
#include <syncstream>
412-
#include <functional>
413-
```
414-
415415
**测试 demo**
416416

417417
```cpp
418-
void print_task(int n) {
418+
int print_task(int n) {
419419
std::osyncstream{ std::cout } << "Task " << n << " is running." << std::endl;
420+
return n;
420421
}
421-
void print_task2(int n) {
422+
int print_task2(int n) {
422423
std::osyncstream{ std::cout } << "🐢🐢🐢 " << n << " 🐉🐉🐉" << std::endl;
424+
return n;
423425
}
424426

425427
int main() {
426428
ThreadPool pool{ 4 }; // 创建一个有 4 个线程的线程池
429+
std::vector<std::future<int>> futures; // future 集合,获取返回值
427430

428431
for (int i = 0; i < 10; ++i) {
429-
pool.submit(print_task, i);
432+
futures.emplace_back(pool.submit(print_task, i));
430433
}
431-
pool.join(); // 确保执行完所有任务
434+
pool.join();
432435

433436
std::puts("---------------------");
434437

435-
pool.start(); // 重新启动线程池
438+
pool.start();
436439

437440
for (int i = 0; i < 10; ++i) {
438-
pool.submit(print_task2, i);
441+
futures.emplace_back(pool.submit(print_task2, i));
439442
}
440-
pool.join(); // 确保执行完所有任务
443+
pool.join();
444+
445+
int sum = 0;
446+
for(auto& future : futures){
447+
sum += future.get();
448+
}
449+
std::cout << "sum: " << sum << '\n';
441450
} // 析构自动 stop() join()
442451
```
443452
444-
**可能的[运行结果](https://godbolt.org/z/rM6vz1s9v)**:
453+
**可能的[运行结果](https://godbolt.org/z/YT4ahh1Wz)**:
445454
446455
```shell
447-
Task 1 is running.
448-
Task 2 is running.
449456
Task 0 is running.
450-
Task 3 is running.
451457
Task 4 is running.
452458
Task 5 is running.
453459
Task 6 is running.
454460
Task 7 is running.
455461
Task 8 is running.
456462
Task 9 is running.
463+
Task 2 is running.
464+
Task 3 is running.
465+
Task 1 is running.
457466
---------------------
458467
🐢🐢🐢 0 🐉🐉🐉
459-
🐢🐢🐢 2 🐉🐉🐉
460468
🐢🐢🐢 1 🐉🐉🐉
469+
🐢🐢🐢 2 🐉🐉🐉
461470
🐢🐢🐢 5 🐉🐉🐉
462-
🐢🐢🐢 7 🐉🐉🐉
463-
🐢🐢🐢 3 🐉🐉🐉
464471
🐢🐢🐢 4 🐉🐉🐉
472+
🐢🐢🐢 3 🐉🐉🐉
465473
🐢🐢🐢 6 🐉🐉🐉
474+
🐢🐢🐢 7 🐉🐉🐉
466475
🐢🐢🐢 8 🐉🐉🐉
467476
🐢🐢🐢 9 🐉🐉🐉
477+
sum: 90
468478
```
469479

470-
如果不自己显式调用 `join()` ,而是等待线程池析构调用,那么效果如同 `asio::thread_pool`,会先进行 `stop`,导致一些任务不执行。
480+
> 如果不自己显式调用 `join()` ,而是等待线程池对象调用析构函数,那么效果如同 `asio::thread_pool`,会先进行 `stop`,导致一些任务不执行。
481+
482+
**构造函数和析构函数:**
483+
484+
- **构造函数**:初始化线程池并**启动线程**
485+
486+
- **析构函数**:停止线程池并等待所有线程结束。
487+
488+
**外部接口:**
489+
490+
- **`stop()`**:停止线程池,通知所有线程退出(不会等待所有任务执行完毕)。
491+
492+
- **`join()`**:等待所有线程完成任务。
493+
494+
- **`submit()`**:将任务提交到任务队列,并返回一个`std::future`对象用于获取任务结果。
495+
496+
- **`start()`**:启动线程池,创建并启动指定数量的线程。

0 commit comments

Comments
 (0)