基于 C++ MultiThread Worker 的方法

标签 c++ multithreading synchronization worker

我正在寻求开发一个基于多线程 worker 的子应用程序。我正在浏览各种站点上的方法,然后突然翻到一个 wiki 页面。

https://en.wikipedia.org/wiki/Thread_pool_pattern

When implementing this pattern, the programmer should ensure thread-safety of the queue. In Java, you can synchronize the relevant method using the synchronized keyword. This will bind the block modified with synchronized into one atomic structure, therefore forcing any threads using the associated resource to wait until there are no threads using the resource.( I hope this is achievable by applying mutex lock in c++) As a drawback to this method, synchronization is rather expensive. You can also create an object that holds a list of all the jobs in a queue, which could be a singleton.

我想了解的是,包含所有作业列表的单例对象如何比同步队列更能帮助我。它们有何不同?

P.S:我知道你们中很少有人会质疑我内容的来源(这里是 wiki),但我仍然想了解更多。

提前致谢。

最佳答案

为什么要“重新发明轮子”而不是仅仅使用像 这样的行业标准方法?或 (哪些是专门针对 C++ 应用程序的)?它们都基于工作窃取调度程序,该调度程序在两个方向上都能很好地扩展(比共享队列好得多),支持嵌套并行性,从而解决了在您发明自己的线程池或使用现有线程池时不可避免的可组合性问题使用固定数量的工作线程(如 )。

关于基于 C++ MultiThread Worker 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32789922/

相关文章:

ios - iOS6 中无法从后台线程调用主线程上的代码

c - 主/工作线程和信号处理

language-agnostic - 原子指令

python - 如何在不阻塞主线程的情况下执行 "fire and forget"任务?

c++ - (C/C++) libjpeg,缺少 "Release.obj"文件

c++ - 使用位

c++ - 按照键优先级的指定顺序按值对映射进行排序

synchronization - AAD 组和 MS 团队同步太慢

javascript - async.js 和系列问题

c++ - 0除以无穷大一定是0吗?