✅ 阶段 4 完整细化包(3 周详细课表 + 线程池代码框架 + 模拟面试题)
以下是针对阶段 4:多线程编程与并发 的完整细化内容。
一、3 周详细课表(Daily Schedule)
总时长:21 天(每天 2.5–4 小时)
每日结构:理论 40min + 编码 1.5–2h + 调试/总结 30min
Week 1:基础 + 同步原语(打牢根基)
- Day 1:
std::thread+join/detach+ 简单并行累加 - Day 2:
std::async/future/promise+ 异常传播 - Day 3:
std::mutex+std::lock_guard+unique_lock(银行账户练习) - Day 4:死锁演示与解决(
std::lock()+ 锁顺序) - Day 5:
std::condition_variable+ 生产者-消费者(单生产者单消费者) - Day 6:多生产者多消费者 + 虚假唤醒处理
- Day 7:Week 1 复习 + 写总结文档 + Thread Sanitizer 使用
Week 2:线程安全容器 + 原子操作
- Day 8:线程安全 Singleton(3 种实现对比)
- Day 9:线程安全队列(
std::deque+ mutex + cv) - Day 10:读写锁(
std::shared_mutex) + 线程安全 LRU Cache - Day 11:
std::atomic基础 + 无锁计数器 +memory_order实验 - Day 12:ABA 问题演示 + 带版本号的指针
- Day 13:
std::call_once+std::once_flag+ 其他同步设施 - Day 14:Week 2 代码重构 + 性能对比测试(单锁 vs 读写锁)
Week 3:线程池核心项目 + 综合实战
- Day 15-17:线程池项目(分 3 天完成)
- Day 15:基础框架 + 任务队列 + 工作线程
- Day 16:
std::future支持返回值 + 优雅关闭 - Day 17:优先级任务 + 动态线程数(可选) + 压力测试
- Day 18:并行计算实战(Monte Carlo 求 π 或 并行文件搜索)
- Day 19:并发 Bug 专项练习(故意制造 Data Race / Deadlock 并修复)
- Day 20:线程池代码 review + 性能优化(减少锁竞争、批量任务等)
- Day 21:阶段总结 + 面试题自测 + 项目文档整理(README)
二、具体项目代码框架 —— 高性能线程池(推荐重点实现)
文件结构建议:
ThreadPool/
├── ThreadPool.h
├── ThreadPool.cpp
├── TaskQueue.h
├── main.cpp
├── CMakeLists.txt
1. TaskQueue.h(线程安全任务队列)
#pragma once
#include <queue>
#include <functional>
#include <mutex>
#include <condition_variable>
class TaskQueue {
public:
using Task = std::function<void()>;
void push(Task task);
Task pop(); // 阻塞式取出
bool try_pop(Task& task); // 非阻塞
void shutdown();
bool is_shutdown() const;
private:
std::queue<Task> queue_;
mutable std::mutex mutex_;
std::condition_variable cv_;
bool shutdown_ = false;
};
2. ThreadPool.h(核心头文件)
#pragma once
#include "TaskQueue.h"
#include <vector>
#include <thread>
#include <future>
#include <atomic>
class ThreadPool {
public:
explicit ThreadPool(size_t thread_count = std::thread::hardware_concurrency());
~ThreadPool();
// 提交任务(支持返回值)
template<typename F, typename... Args>
auto submit(F&& f, Args&&... args)
-> std::future<std::invoke_result_t<F, Args...>>;
void shutdown();
size_t thread_count() const { return workers_.size(); }
private:
void worker_thread(); // 工作线程主函数
TaskQueue task_queue_;
std::vector<std::thread> workers_;
std::atomic<bool> stop_{false};
};
3. ThreadPool.cpp(核心实现框架)
#include "ThreadPool.h"
ThreadPool::ThreadPool(size_t thread_count) {
for (size_t i = 0; i < thread_count; ++i) {
workers_.emplace_back([this] { worker_thread(); });
}
}
ThreadPool::~ThreadPool() {
shutdown();
}
void ThreadPool::worker_thread() {
while (true) {
Task task = task_queue_.pop();
if (task_queue_.is_shutdown() && !task) break; // 退出信号
if (task) task();
}
}
template<typename F, typename... Args>
auto ThreadPool::submit(F&& f, Args&&... args)
-> std::future<std::invoke_result_t<F, Args...>>
{
using ReturnType = std::invoke_result_t<F, Args...>;
auto task = std::make_shared<std::packaged_task<ReturnType()>>(
std::bind(std::forward<F>(f), std::forward<Args>(args)...));
std::future<ReturnType> res = task->get_future();
task_queue_.push([task]() { (*task)(); });
return res;
}
void ThreadPool::shutdown() {
// 实现优雅关闭逻辑
}
实现提示:
shutdown()需要设置标志 +notify_all()+ 等待所有线程结束- 建议先实现无返回值版本,再加上
packaged_task - 后续可增加优先级队列(
std::priority_queue+ 自定义 Task 带 priority)
三、阶段 4 模拟面试题清单(30 道高频题)
基础(必会)
std::thread的join()和detach()区别?什么场景必须 join?- 什么是 RAII?在并发中如何体现?
std::lock_guard与std::unique_lock的区别?- 生产者-消费者模型中为什么要用
while而不是if判断条件?
中级(重点考察)
- 什么是 Data Race?如何用工具检测?
std::atomic的memory_order有哪些?分别什么含义?- 解释 Double-Checked Locking(DCL)实现 Singleton 的完整过程及潜在问题。
shared_ptr在多线程下安全吗?引用计数是如何保证线程安全的?std::condition_variable::notify_one()vsnotify_all()区别及适用场景?
高级 / 项目深挖
- 线程池的核心设计要点有哪些?如何处理任务积压?
- 如何实现一个支持
future返回值的线程池? - 什么是 False Sharing?如何避免?
- 读写锁的实现原理?C++17
std::shared_mutex使用注意事项? - 如何实现无锁队列(Lock-Free Queue)?CAS 需要注意什么?
场景题
- 有一个全局计数器,多线程频繁读写,如何优化性能?
- 如何实现一个异步日志系统(不阻塞业务线程)?
- 死锁的四个必要条件?如何预防?
建议练习方式:
- 每天自答 3-5 题,录音或写在笔记里
- 重点准备“画图讲解”:虚函数表、内存序、线程池架构图
完成这个细化包后,你的多线程能力会非常扎实,完全可以应对大多数大厂 C++ 岗位的并发考察。
下一步你想要哪部分继续细化?
- 线程池完整可编译代码(我可以一次性给出全部实现)
- Week 1 或 Week 3 的每日具体编码任务 + 参考代码
- CMakeLists.txt 完整配置
- 其他阶段(例如阶段 3 STL 或阶段 6 项目)的类似细化
924

被折叠的 条评论
为什么被折叠?



