【问题标题】:folly IOThreadPoolExecutor vs CPUThreadPoolExecutor愚蠢的 IOThreadPoolExecutor 与 CPUThreadPoolExecutor
【发布时间】:2021-07-07 05:12:29
【问题描述】:

我正在尝试详细了解我正在处理的代码库所使用的异步抽象。

我正在阅读 Folly 库中两个异步执行器池的文档,IOThreadPoolExecutor 用于 io 绑定任务,CPUThreadPoolExecutor 用于 cpu 绑定任务 (https://github.com/facebook/folly/blob/master/folly/docs/Executors.md)。

我正在阅读描述,但我不明白主要区别。似乎IOThreadPoolExecutor 是围绕event_fdepoll 循环构建的,而CPUThreadPoolExecutor 使用队列和信号量。

但这并不能告诉我太多好处和权衡。

【问题讨论】:

    标签: c++ linux folly


    【解决方案1】:

    仅当您需要 EventBase 池时,才应使用较高级别的 IPThreadPoolExecutors。如果您需要一个工作池,请使用 CPUThreadPoolExecutor。

    CPUThreadPoolExecutor

    包含一系列优先级队列,这些优先级队列不断被一系列工作人员拾取。每个工作线程在创建后执行 threadRun()。 ThreadRun() 本质上是一个无限循环,它从任务队列中拉出一个任务并执行它。如果任务在获取时已经过期,则执行过期回调而不是任务本身。

    IOThreadPoolExecutor

    每个 IO 线程都运行自己的 EventBase。 IOThreadPoolExecutor 不会像 CPUThreadPoolExecutor 那样从任务队列中拉取任务,而是将事件注册到下一个 IO 线程的 EventBase。然后每个 IO 线程为其 EventBase 调用 loopForEver(),它本质上调用 epoll() 来执行异步 io。

    所以大多数时候您可能应该使用 CPUThreadPoolExecutor,因为这是拥有工人池的常见用例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 2011-08-24
      • 2012-05-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多