All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
Radiant::ThreadPool Class Referenceabstract

Thread pool class that is similar to Thread class, but the childLoop is executed concurrently with many threads. More...

#include <Radiant/ThreadPool.hpp>

Inheritance diagram for Radiant::ThreadPool:
Patterns::NotCopyable Radiant::BGThread

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...
 

Detailed Description

Thread pool class that is similar to Thread class, but the childLoop is executed concurrently with many threads.

See Also
Thread

Constructor & Destructor Documentation

Radiant::ThreadPool::ThreadPool ( )

Construct a thread pool class.

The threads are NOT activated by this method.

virtual Radiant::ThreadPool::~ThreadPool ( )
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).

Member Function Documentation

virtual void Radiant::ThreadPool::childLoop ( )
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.

Returns
true if any of the threads are running
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.

Parameters
numberthe target number of threads
bool Radiant::ThreadPool::running ( ) const
protected

This should only be called from childLoop(), This function is thread-safe.

Returns
true if we should continue running this thread
bool Radiant::ThreadPool::stop ( )

Asks threads to stop.

Doesn't work as expected if the childLoop() implementation doesn't use and obey running().

Returns
true if the stop was successful
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.

Returns
the number of running or starting threads
bool Radiant::ThreadPool::waitEnd ( )

Waits until all threads are finished.

See Also
Thread::waitEnd()
Returns
true if all threads have successfully finished

Member Data Documentation

Radiant::Condition Radiant::ThreadPool::m_wait
protected

Every time when we want to delete a thread, this condition variable will signaled.