Thread pool class that is similar to Thread class, but the childLoop is executed concurrently with many threads. More...
#include <Radiant/ThreadPool.hpp>
Public Member Functions | |
| bool | isRunning () const |
| Returns true if any of the threads are running. More... | |
| void | run (int number=1) |
| Sets the number of threads. More... | |
| bool | stop () |
| Asks threads to stop. More... | |
| ThreadPool () | |
| Construct a thread pool class. More... | |
| int | threads () const |
| Returns the number of running or starting threads. More... | |
| bool | waitEnd () |
| Waits until all threads are finished. More... | |
| virtual | ~ThreadPool () |
| Destructor. More... | |
Protected Member Functions | |
| virtual void | childLoop ()=0 |
| The actual contents of the threads. More... | |
| bool | running () const |
| This should only be called from childLoop(), This function is thread-safe. More... | |
| virtual void | wakeAll () |
| Wakes all threads to perform their duties. | |
Protected Attributes | |
| Radiant::Mutex | m_mutexWait |
| Mutex to be used with m_wait. | |
| Radiant::Condition | m_wait |
| Every time when we want to delete a thread, this condition variable will signaled. More... | |
Thread pool class that is similar to Thread class, but the childLoop is executed concurrently with many threads.
| Radiant::ThreadPool::ThreadPool | ( | ) |
Construct a thread pool class.
The threads are NOT activated by this method.
|
virtual |
Destructor.
The thread must be stopped before this method is
called. Thread cannot be terminated within the destructor, as the inheriting class that implements the virtual childLoop function does not exist any more (its destructor is called before this function).
|
protectedpure virtual |
The actual contents of the threads.
You need to override this to add functionality to your software. This will be called once per thread at the same time.
| bool Radiant::ThreadPool::isRunning | ( | ) | const |
Returns true if any of the threads are running.
Not to be confused with running() This function is thread-safe.
| void Radiant::ThreadPool::run | ( | int | number = 1 | ) |
Sets the number of threads.
If number is bigger than threads(), new threads are started immediately If number is smaller, then we politely give a hint to randomly chosen "extra" threads to shut down (ThreadPool::stop).
This can be freely called many times.
This function is thread-safe.
| number | the target number of threads |
|
protected |
This should only be called from childLoop(), This function is thread-safe.
| bool Radiant::ThreadPool::stop | ( | ) |
Asks threads to stop.
Doesn't work as expected if the childLoop() implementation doesn't use and obey running().
| int Radiant::ThreadPool::threads | ( | ) | const |
Returns the number of running or starting threads.
Doesn't count threads that have been asked to quit. This function is thread-safe.
| bool Radiant::ThreadPool::waitEnd | ( | ) |
Waits until all threads are finished.
|
protected |
Every time when we want to delete a thread, this condition variable will signaled.