35 #ifndef _BLAZE_UTIL_THREADPOOL_THREADPOOL_H_
36 #define _BLAZE_UTIL_THREADPOOL_THREADPOOL_H_
44 #include <boost/bind.hpp>
306 template<
typename TT
310 class ThreadPool :
private NonCopyable
341 inline size_t size()
const;
342 inline size_t active()
const;
343 inline size_t ready()
const;
350 template<
typename Callable >
353 template<
typename Callable,
typename A1 >
354 void schedule( Callable func, A1 a1 );
356 template<
typename Callable,
typename A1,
typename A2 >
357 void schedule( Callable func, A1 a1, A2 a2 );
359 template<
typename Callable,
typename A1,
typename A2,
typename A3 >
360 void schedule( Callable func, A1 a1, A2 a2, A3 a3 );
362 template<
typename Callable,
typename A1,
typename A2,
typename A3,
typename A4 >
363 void schedule( Callable func, A1 a1, A2 a2, A3 a3, A4 a4 );
365 template<
typename Callable,
typename A1,
typename A2,
typename A3,
typename A4,
typename A5 >
366 void schedule( Callable func, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5 );
373 void resize(
size_t n,
bool block=
false );
406 friend class Thread<TT,MT,LT,CT>;
429 template<
typename TT
462 template<
typename TT
477 waitForTask_.notify_all();
480 while( total_ != 0 ) {
481 waitForThread_.wait( lock );
485 for(
typename Threads::Iterator thread=threads_.begin(); thread!=threads_.end(); ++thread ) {
508 template<
typename TT
515 return taskqueue_.isEmpty();
525 template<
typename TT
542 template<
typename TT
559 template<
typename TT
566 return expected_ - active_;
588 template<
typename TT
592 template<
typename Callable >
596 taskqueue_.push( func );
597 waitForTask_.notify_one();
612 template<
typename TT
616 template<
typename Callable
621 taskqueue_.push( boost::bind<void>( func, a1 ) );
622 waitForTask_.notify_one();
638 template<
typename TT
642 template<
typename Callable
648 taskqueue_.push( boost::bind<void>( func, a1, a2 ) );
649 waitForTask_.notify_one();
666 template<
typename TT
670 template<
typename Callable
677 taskqueue_.push( boost::bind<void>( func, a1, a2, a3 ) );
678 waitForTask_.notify_one();
696 template<
typename TT
700 template<
typename Callable
708 taskqueue_.push( boost::bind<void>( func, a1, a2, a3, a4 ) );
709 waitForTask_.notify_one();
728 template<
typename TT
732 template<
typename Callable
741 taskqueue_.push( boost::bind<void>( func, a1, a2, a3, a4, a5 ) );
742 waitForTask_.notify_one();
795 template<
typename TT
802 #if !(defined _MSC_VER)
804 throw std::invalid_argument(
"Invalid number of threads" );
812 if( n > expected_ ) {
813 for(
size_t i=expected_; i<n; ++i )
820 waitForTask_.notify_all();
822 while( block && total_ != expected_ ) {
823 waitForThread_.wait( lock );
828 for(
typename Threads::Iterator thread=threads_.begin(); thread!=threads_.end(); ) {
829 if( thread->hasTerminated() ) {
831 thread = threads_.erase( thread );
847 template<
typename TT
855 while( !taskqueue_.isEmpty() || active_ > 0 ) {
856 waitForThread_.wait( lock );
870 template<
typename TT
895 template<
typename TT
918 template<
typename TT
930 while( taskqueue_.isEmpty() )
933 waitForThread_.notify_all();
935 if( total_ > expected_ ) {
940 waitForTask_.wait( lock );
945 task = taskqueue_.pop();
size_t size() const
Returns the current size of the thread pool.
Definition: ThreadPool.h:529
Thread< TT, MT, LT, CT > ManagedThread
Type of the managed threads.
Definition: ThreadPool.h:314
void schedule(Callable func)
Scheduling the given zero argument function/functor for execution.
Definition: ThreadPool.h:593
Header file for basic type definitions.
Condition waitForTask_
Wait condition for idle threads.
Definition: ThreadPool.h:399
void clear()
Removing all scheduled tasks from the thread pool.
Definition: ThreadPool.h:874
Base class for non-copyable class instances.
void createThread()
Adding a new thread to the thread pool.
Definition: ThreadPool.h:899
Mutex mutex_
Synchronization mutex.
Definition: ThreadPool.h:398
void resize(size_t n, bool block=false)
Changes the total number of threads in the thread pool.
Definition: ThreadPool.h:799
size_t active() const
Returns the number of currently active/busy threads.
Definition: ThreadPool.h:546
volatile size_t active_
Number of currently active/busy threads.
Definition: ThreadPool.h:395
bool executeTask()
Executing a scheduled task.
Definition: ThreadPool.h:922
LT Lock
Type of a locking object.
Definition: ThreadPool.h:318
Implementation of a vector for (polymorphic) pointers.
volatile size_t expected_
Expected number of threads in the thread pool.
Definition: ThreadPool.h:392
Task queue for the thread pool.
~ThreadPool()
Destructor for the ThreadPool class.
Definition: ThreadPool.h:466
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
Header file for the Thread class.
Condition waitForThread_
Wait condition for the thread management.
Definition: ThreadPool.h:400
bool isEmpty() const
Returns whether any tasks are scheduled for execution.
Definition: ThreadPool.h:512
Implementation of a single thread of execution.
Definition: Thread.h:250
Implementation of an iterator for pointer vectors.The PtrIterator class follows the example of the ra...
Definition: PtrIterator.h:108
MT Mutex
Type of the mutex.
Definition: ThreadPool.h:317
ThreadPool(size_t n)
Constructor for the ThreadPool class.
Definition: ThreadPool.h:433
PtrVector< ManagedThread > Threads
Type of the thread container.
Definition: ThreadPool.h:315
Header file for run time assertion macros.
Threads threads_
The threads contained in the thread pool.
Definition: ThreadPool.h:396
threadpool::TaskQueue TaskQueue
Type of the task queue.
Definition: ThreadPool.h:316
Header file for the Task base class.
boost::function< void(void)> Task
Handle for a single, executable task.
Definition: Task.h:60
void wait()
Waiting for all scheduled tasks to be completed.
Definition: ThreadPool.h:851
volatile size_t total_
Total number of threads in the thread pool.
Definition: ThreadPool.h:391
Task queue for the thread pool.The TaskQueue class represents the internal task container of a thread...
Definition: TaskQueue.h:64
size_t ready() const
Returns the number of currently ready/inactive threads.
Definition: ThreadPool.h:563
TaskQueue taskqueue_
Task queue for the scheduled tasks.
Definition: ThreadPool.h:397
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.In case of an invalid run time expression, the program execution is terminated. The BLAZE_INTERNAL_ASSERT macro can be disabled by setting the BLAZE_USER_ASSERTION flag to zero or by defining NDEBUG during the compilation.
Definition: Assert.h:101
CT Condition
Condition variable type.
Definition: ThreadPool.h:319