#include <iostream>
#include <vector>
#include <deque>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <memory>
#include <atomic>
#include <future>
#include <type_traits>
#include <chrono>
// ==========================================
// 1. 工作窃取队列 (Work-Stealing Queue)
// ==========================================
class work_stealing_queue {
private:
using task_type=std::function<void()> ;
std::deque<task_type> the_data;
mutable std::mutex mtx; // 保护双端队列的并发访问
public:
work_stealing_queue() = default;
work_stealing_queue(const work_stealing_queue&) = delete;
work_stealing_queue& operator=(const work_stealing_queue&) = delete;
// 所有者:推入任务到队头 (LIFO,利用缓存局部性)
void push(task_type data) {
std::lock_guard<std::mutex> lock(mtx);
the_data.push_front(std::move(data));
}
// 所有者:从队头取出任务
bool pop(task_type& data) {
std::lock_guard<std::mutex> lock(mtx);
if (the_data.empty()) return false;
data = std::move(the_data.front());
the_data.pop_front();
return true;
}
// 窃取者:从队尾窃取任务 (FIFO,减少与所有者的锁竞争)
bool steal(task_type& data) {
std::lock_guard<std::mutex> lock(mtx);
if (the_data.empty()) return false;
data = std::move(the_data.back());
the_data.pop_back();
return true;
}
};
// ==========================================
// 2. 线程池 (Thread Pool)
// ==========================================
class thread_pool {
private:
typedef std::function<void()> task_type;
std::atomic_bool done; // 线程池是否停止
std::vector<std::unique_ptr<work_stealing_queue>> queues; // 局部队列池
std::vector<std::thread> threads; // 线程句柄池
std::deque<task_type> global_queue; // 全局任务队列
std::mutex global_mtx;
std::condition_variable global_cv;
// thread_local 变量:每个工作线程独享自己的局部队列指针和索引
static thread_local work_stealing_queue* local_work_queue;
static thread_local unsigned my_index;
// 线程工作主循环
void worker_thread(unsigned index) {
my_index = index;
// 绑定当前线程与专属队列
local_work_queue = queues[index].get();
while (!done) {
run_pending_task();
}
}
// 尝试从本地队列获取
bool pop_task_from_local_queue(task_type& task) {
return local_work_queue && local_work_queue->pop(task);
}
// 尝试从全局队列获取
bool pop_task_from_global_queue(task_type& task) {
std::lock_guard<std::mutex> lock(global_mtx);
if (global_queue.empty()) return false;
task = std::move(global_queue.front());
global_queue.pop_front();
return true;
}
// 尝试从其他线程的队列窃取
bool pop_task_from_other_thread_queue(task_type& task) {
// 从当前线程的下一个开始轮询,错开起点,减少竞争
for (unsigned i = 0; i < queues.size(); ++i) {
unsigned const index = (my_index + i + 1) % queues.size();
if (queues[index]->steal(task)) {
return true;
}
}
return false;
}
// 执行任务的核心调度逻辑
void run_pending_task() {
task_type task;
// 优先级:本地队列 -> 全局队列 -> 窃取其他线程
if (pop_task_from_local_queue(task) ||
pop_task_from_global_queue(task) ||
pop_task_from_other_thread_queue(task))
{
task(); // 执行任务
}
else {
// 所有队列都空了,线程挂起等待全局队列的新任务
std::unique_lock<std::mutex> lock(global_mtx);
global_cv.wait(lock, [this] {
return done || !global_queue.empty();
});
}
}
public:
thread_pool() : done(false) {
unsigned const thread_count = std::thread::hardware_concurrency();
try {
// 初始化所有的局部队列
for (unsigned i = 0; i < thread_count; ++i) {
queues.push_back(std::make_unique<work_stealing_queue>());
}
// 启动线程
for (unsigned i = 0; i < thread_count; ++i) {
threads.push_back(std::thread(&thread_pool::worker_thread, this, i));
}
}
catch (...) {
done = true;
throw;
}
}
~thread_pool() {
done = true;
global_cv.notify_all(); // 唤醒所有处于 wait 状态的线程
for (std::thread& t : threads) {
if (t.joinable())
t.join();
}
}
// 提交任务接口(支持返回值)
template<typename FunctionType,typename ... ARGS>
auto submit(FunctionType&& f,ARGS&&...args)
{
auto bound_task = std::bind(std::forward<FunctionType>(f), std::forward<ARGS>(args)...);
using result_type = decltype(bound_task());
auto task = std::make_shared<std::packaged_task<result_type()>>(std::move(bound_task));
auto res = task->get_future();
// 【核心修正:区分提交者身份】
if (local_work_queue) {
// 情况 A:是线程池内部的工作线程在提交任务
// 直接推入自己的局部队列,享受无锁竞争和数据局部性优势
local_work_queue->push([task]() { (*task)(); });
}
else {
// 情况 B:是外部线程(如主线程)在提交任务
// 推入全局队列并唤醒一个沉睡的工作线程
{
std::lock_guard<std::mutex> lock(global_mtx);
global_queue.emplace_back([task]() {(*task)();});
}
global_cv.notify_one();
}
return res;
}
};
// 静态成员变量定义
thread_local work_stealing_queue* thread_pool::local_work_queue = nullptr;
thread_local unsigned thread_pool::my_index = 0;
// ==========================================
// 3. 测试代码
// ==========================================
int main() {
thread_pool pool;
std::cout << "Thread pool started with "
<< std::thread::hardware_concurrency() << " threads." << std::endl;
std::vector<std::future<int>> results;
// 1. 外部主线程提交 20 个初始任务(会进入全局队列)
for (int i = 0; i < 20; ++i) {
results.push_back(pool.submit([i]() {
std::this_thread::sleep_for(std::chrono::milliseconds(20));
return i * i;
}));
}
//这里还是提交到全局队列,因为还是在主线程中进行任务提交的,所以对应的线程局部对象默认为null 即便同一个线程池对象,传递给不同的线程访问其线程局部对象,访问的都是各自线程的局部对象
auto complex_task = pool.submit([]() {
std::cout << "[Worker Thread] Starting complex task, submitting sub-tasks to local queue..." << std::endl;
return 999;
});
// 2. 【真正测试内部提交】父任务在工作线程中执行,并派生子任务
auto future_outer = pool.submit([&pool]() {
std::cout << "\n[Parent Task] Running on worker thread. "
<< "This task was submitted to the GLOBAL queue.\n";
// 此时!当前线程是工作线程,它的 local_work_queue 是有效的!
// 因此下面这行 submit,会触发我们的【内部提交】分支逻辑
auto future_inner = pool.submit([]() {
std::cout << "[Inner Task] I was submitted from a worker thread! "
<< "I am in the LOCAL queue (or stolen by a friend).\n";
return 123;
});
std::cout << "[Parent Task] Sub-task submitted to LOCAL queue. Waiting for it...\n";
// 注意:如果这里get(),而线程池只有1个线程,会导致死锁。
// 但硬件线程数通常>1,其他线程会窃取这个子任务并执行。
return future_inner.get();
});
// 获取结果
long long sum = 0;
for (auto& fut : results) {
sum += fut.get();
}
std::cout << "Sum of squares: " << sum << std::endl;
std::cout << "Complex task result: " << complex_task.get() << std::endl;
std::cout << "Outer task result (which is inner task result): " << future_outer.get() << std::endl;
std::cout << "Main thread waiting for pool destruction..." << std::endl;
return 0;
}