We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.
Closed 9 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
我致力于制作一些我能够使用的东西,并将其发布在 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.
如果您的目标是轻量级,那么您最不想要的就是预先编写的、超通用的、基于高级抽象的实现。自己实现一个适合您的特定任务的线程池是相当简单的,但您可能还会疑问您是否真的需要一个线程池,或者您是否可以根据需要创建和销毁线程。
在不了解有关您的申请的更多详细信息的情况下,我无法提供更具体的建议。但您可能会发现有用的工具有:
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:
这里是一个实现,具有以下功能:
Here is an implementation with these features:
我曾经使用过这个,这实际上并不是一个正式的实现本身。它确实按照您的要求使用 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 theMakefile
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.