C语言 现有线程池 C 实现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6297428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:52:52  来源:igfitidea点击:

Existing threadpool C implementation

cmultithreadingopen-sourcepthreadsthreadpool

提问by Mathias Brossard

What open-source implementation(s) in C for a pthreads thread pool would you recommend ?

您会推荐哪些 C 语言中用于 pthreads 线程池的开源实现?

Additional points if this implementation is :

如果此实现是:

  • Light-weight: glib, APR, NSPR and others come with a big buy-in, I'd rather have just 2 files (header and implementation).
  • Tested on several platforms (Linux, BSD, Mac OS X, etc.).
  • Still maintained.
  • 轻量级:glib、APR、NSPR 和其他人都支持,我宁愿只有 2 个文件(头文件和实现)。
  • 在多个平台(Linux、BSD、Mac OS X 等)上测试。
  • 还保持着。

采纳答案by Mathias Brossard

I worked on making something I'd be able to use and I've published it on github: it's unimaginably called threadpool.

我致力于制作一些我可以使用的东西,并将它发布在 github 上:它被称为threadpool ,这是难以想象的。

回答by R.. GitHub STOP HELPING ICE

If your goal is light-weight, then the last thing you want is a prewritten, super-general-purpose, high-level-abstraction-based implementation. Implementing a thread pool yourself, suited to your particular task, is fairly trivial, but you might also question whether you actually need a thread pool or whether you'd be fine just creating and destroying threads as needed.

如果您的目标是轻量级,那么您最不想要的就是预先编写的、超通用的、基于高级抽象的实现。自己实现一个适合您的特定任务的线程池是相当简单的,但您可能还会质疑您是否真的需要一个线程池,或者您是否可以根据需要创建和销毁线程。

Without knowing more details about your application, I can't give much more specific advice. But the tools you might find useful are:

在不了解有关您的应用程序的更多详细信息的情况下,我无法提供更具体的建议。但是您可能会发现有用的工具是:

  • Condition variables
  • Semaphores
  • A job queue protected by a mutex
  • POSIX message queues
  • 条件变量
  • 信号量
  • 受互斥锁保护的作业队列
  • POSIX 消息队列

回答by Pithikos

Here is an implementationwith these features:

这是具有这些功能的实现

  • ANSI C and POSIX compliant
  • Minimal but powerful API
  • Synchronisation from the user
  • Full documentation
  • ANSI C 和 POSIX 兼容
  • 最小但功能强大的 API
  • 来自用户的同步
  • 完整的文档

回答by Arka

I once used this, which isn't actually an official implementation per se. It does use pthreads as you requested, and should give you some ideas of what you need to do. (See threadpool.h, threadpool.c, threadpool_test.c, and the Makefilefor instructions on how to compile.) You'll obviously have to do some refactoring as it's original intentionis probably different than yours. It's commented rather well actually.

我曾经使用过this,它本身实际上并不是官方实现。它确实按照您的要求使用 pthreads,并且应该给您一些关于您需要做什么的想法。(请参阅threadpool.hthreadpool.cthreadpool_test.cMakefile有关如何编译的说明。)显然您必须进行一些重构,因为它的初衷可能与您的不同。其实评论的还不错。

Even though this deviates from the original question, I'd also like to mention that the newest C standard, unofficially C1X (see wikipedia, hyperlink limit), has planned support for threads N1570 (google it, hyperlink limit again!) (7.31.15).

即使这偏离了最初的问题,我也想提一下,最新的 C 标准,非正式的 C1X(参见维基百科,超链接限制),已经计划支持线程 N1570(谷歌它,再次超链接限制!)(7.31. 15)。

Some personal advice from my experience would be to make sure that your application canactually be run in parallel, and if the overhead of creating a new thread is so high that you can't live without a thread pool. Personally I've blundered on both these parts and I've actually ended up with implementations slower than my single threaded application. Also, you might want to be aware of different problems, including cache-lockouts and misses, which would actually degrade the performance of your application.

从我的经验,一些个人的建议是,以确保您的应用程序能够真正并行运行,如果创建一个新线程的开销是如此之高,你不能没有一个线程池。就我个人而言,我在这两个部分都犯了错误,实际上我的实现速度比我的单线程应用程序慢。此外,您可能希望了解不同的问题,包括缓存锁定和未命中,这实际上会降低应用程序的性能。

I'm probably blabbering on by now, but best of luck.

我现在可能正在喋喋不休,但祝你好运。