我有一个问题,在非递归的情况下,线程池很好地解决了这个问题,并且它将从任务(赋予池的工作/函数)中受益匪浅,能够向池中添加更多任务。我的线程池实现的问题是,第一级任务填充所有工作线程,创建第二级任务,然后在等待第二级完成任务时进行阻塞。由于所有工作线程都被阻塞,等待第二级完成,因此第二级任务永远不会执行,因此整个程序会死锁。
对此有什么通用的解决方案吗?可能是一个抢占线程池(如果可能的话)。我确实考虑过明确区分任务的优先级,但问题是它不能自动处理依赖关系;它需要对API用户做更多的工作。
提前感谢您的任何见解或建议。
EDIT:线程池类定义
class{
public:
thread_pool() = delete;
thread_pool(const thread_pool&) = delete;
thread_pool(unsigned int threads);
~thread_pool();
template<class T, class... Args>
std::future<T>
async(std::function<T(Args...)>&& f, Args&&... args);
template<class... Args>
std::future<void>
async(std::function<void(Args...)>&& f, Args&&... args);
template<class T>
std::future<T>
async(std::function<T()>&& f);
std::future<void>
async(std::function<void()>&& f);
protected:
void init_threads();
void join_threads();
};