Comments (2)
我觉得上面第一个问题并不存在,注意这里
_task_cv.wait(lock, [this]{
return !_run || !_tasks.empty();
}); // wait 直到有 task
- 条件变量的等待是有条件的。
_task_cv.wait
的条件就包括了任务队列不为空。 - 像你说的那种情况,如果多个commit同时发生,只唤醒了一个进程,那么该进程会一直执行到
_tasks
任务队列的所有任务全部执行完才会陷入wait阻塞。 - 另外,你所说的这种情况叫做条件变量的唤醒丢失(可以查查资料),而
wait
函数里的断言就是为了防止这情况发生。
但是第二个问题我觉得确实存在,毕竟stl::vector不是线程安全的。
就我感觉的话,这个线程池最好不要开THREADPOOL_AUTO_GROW
,因为
- 他对线程不安全的
_pool
并没有加锁(致命问题) - 这个
THREADPOOL_AUTO_GROW
只能自动增长,而不能自动减少,甚至连减少线程数量的函数接口都没有,也就是说这个线程数量没法管理,那这样的话还不如一开始直接定好线程数量。(这条纯属个人想法,比如说突然同时来了12个并发commit,导致开了12个线程,结果后面都并发量大大降低,这时开这么多线程感觉多少有点浪费)
from threadpool.
谢谢, 是有必要加锁和自动减少空闲线程 commit 2e0c5e8
from threadpool.
Related Issues (20)
- addThread 工作线程函数97行99行
- 请问怎么循环实现,执行线程池中的几个线程 HOT 1
- AUTO_GROW的线程大多时候创建完就退出了 HOT 1
- threadpool初始化就抛出异常了 HOT 1
- 子线程返回方式矛盾 HOT 1
- 发现一个BUG
- ‘bind’ was not declared in this scope HOT 1
- thread pool 与 numactl 同时使用,出现捕获不到commit的tasks HOT 1
- 自动增长锁,必须有AUTO_GROW宏才会定义,但是在ifndef的判断后使用了它 HOT 3
- 长时间运行后Threadpool为什么会变慢? HOT 1
- 关于 auto task = std::make_shared<std::packaged_task<ResType()>>(std::bind(std::forward<F>(f), std::forward<Args>(args)...) ); HOT 1
- 向 namespace std 添加内容是ub
- 你真的有运行过你的代码吗? HOT 1
- 显示 未定义标识符 "_lockGrow"
- error: no instance of function template "std::threadpool::commit" matches the argument list HOT 1
- idlThrNum是原子变量,是否不需要加锁? HOT 1
- 发现一个bug HOT 3
- 关于自动释放线程的一个问题
- 单例模式
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from threadpool.