-
Notifications
You must be signed in to change notification settings - Fork 1
ThreadPool Usage Guidelines
-
#include ThreadJob.hand#include JobQueue.hif not already included - Make a
threadJobobject and pass it the functions and params you want it to run, but cast the function toEntryPoint*. If there are no params, just give itnullptr. Please also give it a name, as it's helpful for debugging. - You can also give it a 'Priority' depending on whether or not it needs to be run ASAP, though this is not necessary.
- Get the
JobQueueand call itspostJob()function, passing it a reference to the newly created threadJob object
- The job is now on the queue!
- Threads will automatically grab jobs from the queue when they don't have one to do!
- Available threads are all made at runtime, so don't worry about them.
- If you need to pass in multiple parameters, they can be condensed into a pair or tuple. Just make sure to cast the overall object to
void*as is the expected input type for thread job parameters.
-
The threads assume that they are working in a thread-safe environment, it is up to you to provide that.
-
Lazy-instantiated singletons are not thread-safe by default, please refer to
ThreadPool.hand.cppfor thread-safe singletons. -
Threads are self-contained worker threads that will seek for jobs to do on the
JobQueue. You should use theJobQueueandThreadJobto interact with them. -
Not everything is critical, critical should be reserved for time sensitive tasks.
-
ThreadPool is a singleton that holds the references to the worker threads.
-
JobQueue is a singleton containing the priority queue of jobs for threads to do.
-
ThreadJob is a wrapper for the function that a thread should run.
-
Thread is a wrapper for a
std::thread -
ThreadPool is lazily instantiated, so it's
getInstance()has to be called. -
Threads are self-contained worker threads that will seek for jobs to do on the
JobQueue.
-
Using conditional variables to awake threads when a job is place on the queue instead of the current sleeping/checking cycle
-
Exposing queue size (possibly in reference to number of threads) to ensure worker threads are not overworked and allow callers to defer work to the main thread.