Giter VIP home page Giter VIP logo

Comments (2)

williamWANG0325 avatar williamWANG0325 commented on June 13, 2024

我觉得上面第一个问题并不存在,注意这里

_task_cv.wait(lock, [this]{
	return !_run || !_tasks.empty();
}); // wait 直到有 task
  • 条件变量的等待是有条件的。_task_cv.wait的条件就包括了任务队列不为空。
  • 像你说的那种情况,如果多个commit同时发生,只唤醒了一个进程,那么该进程会一直执行到_tasks任务队列的所有任务全部执行完才会陷入wait阻塞。
  • 另外,你所说的这种情况叫做条件变量的唤醒丢失(可以查查资料),而wait函数里的断言就是为了防止这情况发生。

但是第二个问题我觉得确实存在,毕竟stl::vector不是线程安全的。
就我感觉的话,这个线程池最好不要开THREADPOOL_AUTO_GROW,因为

  1. 他对线程不安全的_pool并没有加锁(致命问题)
  2. 这个THREADPOOL_AUTO_GROW只能自动增长,而不能自动减少,甚至连减少线程数量的函数接口都没有,也就是说这个线程数量没法管理,那这样的话还不如一开始直接定好线程数量。(这条纯属个人想法,比如说突然同时来了12个并发commit,导致开了12个线程,结果后面都并发量大大降低,这时开这么多线程感觉多少有点浪费)

from threadpool.

lzpong avatar lzpong commented on June 13, 2024

谢谢, 是有必要加锁和自动减少空闲线程 commit 2e0c5e8

from threadpool.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.