Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Existing threadpool C implementation [closed]

What 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.
like image 628
Mathias Brossard Avatar asked Jun 09 '11 18:06

Mathias Brossard


People also ask

How do you close a ThreadPool?

shutdownNow() should be used to shutdown the thread pool to gracefully exiting the application.

When should you not use ThreadPool?

Thread pools do not make sense when you need thread which perform entirely dissimilar and unrelated actions, which cannot be considered "jobs"; e.g., One thread for GUI event handling, another for backend processing. Thread pools also don't make sense when processing forms a pipeline.

What is a ThreadPool and why is it necessary?

A thread pool helps mitigate the issue of performance by reducing the number of threads needed and managing their lifecycle. Essentially, threads are kept in the thread pool until they're needed, after which they execute the task and return the pool to be reused later.

How do you implement a ThreadPool?

The thread pool implementation consists of two parts. A ThreadPool class which is the public interface to the thread pool, and a PoolThread class which implements the threads that execute the tasks. To execute a task the method ThreadPool. execute(Runnable r) is called with a Runnable implementation as parameter.


2 Answers

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

like image 159
Mathias Brossard Avatar answered Oct 07 '22 23:10

Mathias Brossard


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
like image 39
R.. GitHub STOP HELPING ICE Avatar answered Oct 07 '22 23:10

R.. GitHub STOP HELPING ICE