Existing threadpool C implementation [closed]
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this questionWhat open-source implementation(s) 开发者_如何学运维in C for a pthreads thread pool would you recommend ?
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.
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:
- Condition variables
- Semaphores
- A job queue protected by a mutex
- POSIX message queues
Here is an implementation with these features:
- ANSI C and POSIX compliant
- Minimal but powerful API
- Synchronisation from the user
- Full documentation
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.
精彩评论