I’m programming in C++, but I’m only using pthread.h, no boost or C++11 threads.
So I’m trying to use threads but based on one of my previous questions (link), this doesn’t seem feasible since threads terminate right after completion of its task, and one of the more prevalent reasons to use a thread-pool implementation is to reduce thread-creation overhead by reusing these threads for multiple tasks.
So is the only other way to implement this in C to use fork(), and create a pipe from the main to child processes? Or is there a way to set up a pipe between threads and their parent that I don’t know about?
Many thanks in advance!
Yes, you can create a thread-safe queue between the threads. Then the threads in the pool will sit in a loop retrieving an item from the queue, executing whatever it needs, then going back and getting another.
That’s generally a bit easier/simpler in C++ because it’s a little easier to agree on some of the interface (e.g., overload
operator()to execute the code for a task), but at a fundamental level you can do all the same things in C (e.g., eachtaskstruct you put in the queue will contain a pointer to a function to carry out the work for that task).In your case, since you are using C++, it’s probably easier to use an overload of
operator()to do the work though. The rest of thetaskstruct (or whatever you choose to call it) will contain any data needed, etc.