现有线程池 C 实现

发布于 2024-11-14 18:17:38 字数 1539 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

有木有妳兜一样 2024-11-21 18:17:38

我致力于制作一些我能够使用的东西,并将其发布在 github 上:它的名字令人难以想象 threadpool。

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

快乐很简单 2024-11-21 18:17:38

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

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

  • 条件变量
  • 信号量
  • 受互斥体保护的作业队列
  • POSIX 消息队列

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
情话已封尘 2024-11-21 18:17:38

这里是一个实现,具有以下功能:

  • 符合 ANSI C 和 POSIX 标准
  • 最小但功能强大的 API
  • 同步用户
  • 完整文档

Here is an implementation with these features:

  • ANSI C and POSIX compliant
  • Minimal but powerful API
  • Synchronisation from the user
  • Full documentation
Spring初心 2024-11-21 18:17:38

我曾经使用过这个,这实际上并不是一个正式的实现本身。它确实按照您的要求使用 pthreads,并且应该为您提供一些关于您需要做什么的想法。 (有关如何编译。)显然你必须做一些重构 它的初衷可能与你的不同。其实评论得还不错。

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

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

我现在可能还在胡言乱语,但祝你好运。

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 Makefile for instructions on how to compile.) You'll obviously have to do some refactoring as it's original intention is probably different than yours. It's commented rather well actually.

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).

Some personal advice from my experience would be to make sure that your application can actually 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文