-
Notifications
You must be signed in to change notification settings - Fork 351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
发现一个bug #29
Comments
草, 我就说不光我一个人发现了这个问题.... |
找到解决方案了, 与其让当前线程直接停止工作, 不如我们让当前线程等待, 因此我们可以使用条件变量来进行. 具体的代码如下: if (this->auto_release_free_thread && this->_free_thread_counter > this->init_thread_counter) {
std::unique_lock<std::mutex> __thread_lock(this->_threads_lock);
this->_thread_cv.wait(__thread_lock, [this]() -> bool {
return this->_free_thread_counter <= this->init_thread_counter;
});
} |
这个线程池不仅这一个bug,我刚测试了一下,线程池的动态添加线程功能不是很理想,我把线程池的最大数量设置为5,初始化时线程池的容量设置为4。当我连续添加五个function(每个function睡眠2s),就会发现第五个线程并没有动态增加一个线程来处理任务,而是等其中一个线程处理完才会处理第五个任务。归根结底,在commit的时候,空闲线程数量计数的时机有bug。 此外,线程池偶尔会无法正常退出,不知道哪里出现了问题。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
理论上线程被释放之后——pool.size()应该减少一个,但是代码中没有删除_pool中元素的操作导致_pool的大小只增不减,这时如果输入压力增大线程数会因为达到上限而不开辟新线程,极端情况下甚至会导致线程清零(因为删除操作不使计数器减少导致"_pool.size() > _initSize"失效)从而没有线程执行新任务。
The text was updated successfully, but these errors were encountered: