35 #ifndef _BLAZE_UTIL_THREADPOOL_THREADPOOL_H_
36 #define _BLAZE_UTIL_THREADPOOL_THREADPOOL_H_
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,
typename... Args >
351 void schedule( Callable func, Args&&... args );
358 void resize(
size_t n,
bool block=
false );
391 friend class Thread<TT,MT,LT,CT>;
414 template<
typename TT
447 template<
typename TT
462 waitForTask_.notify_all();
465 while( total_ != 0UL ) {
466 waitForThread_.wait( lock );
470 for(
typename Threads::Iterator thread=threads_.begin(); thread!=threads_.end(); ++thread ) {
493 template<
typename TT
500 return taskqueue_.isEmpty();
510 template<
typename TT
527 template<
typename TT
544 template<
typename TT
551 return expected_ - active_;
575 template<
typename TT
579 template<
typename Callable
584 taskqueue_.push( std::bind<void>( func, std::forward<Args>( args )... ) );
585 waitForTask_.notify_one();
638 template<
typename TT
645 #if !(defined _MSC_VER)
656 if( n > expected_ ) {
657 for(
size_t i=expected_; i<n; ++i )
664 waitForTask_.notify_all();
666 while( block && total_ != expected_ ) {
667 waitForThread_.wait( lock );
672 for(
typename Threads::Iterator thread=threads_.begin(); thread!=threads_.end(); ) {
673 if( thread->hasTerminated() ) {
675 thread = threads_.erase( thread );
691 template<
typename TT
699 while( !taskqueue_.isEmpty() || active_ > 0UL ) {
700 waitForThread_.wait( lock );
714 template<
typename TT
739 template<
typename TT
762 template<
typename TT
774 while( taskqueue_.isEmpty() )
777 waitForThread_.notify_all();
779 if( total_ > expected_ ) {
784 waitForTask_.wait( lock );
789 task = taskqueue_.pop();
#define BLAZE_THROW_INVALID_ARGUMENT(MESSAGE)
Macro for the emission of a std::invalid_argument exception.This macro encapsulates the default way o...
Definition: Exception.h:235
size_t size() const
Returns the current size of the thread pool.
Definition: ThreadPool.h:514
Thread< TT, MT, LT, CT > ManagedThread
Type of the managed threads.
Definition: ThreadPool.h:314
Header file for basic type definitions.
Condition waitForTask_
Wait condition for idle threads.
Definition: ThreadPool.h:384
void clear()
Removing all scheduled tasks from the thread pool.
Definition: ThreadPool.h:718
Header file for exception macros.
Base class for non-copyable class instances.
void createThread()
Adding a new thread to the thread pool.
Definition: ThreadPool.h:743
Mutex mutex_
Synchronization mutex.
Definition: ThreadPool.h:383
void resize(size_t n, bool block=false)
Changes the total number of threads in the thread pool.
Definition: ThreadPool.h:642
std::function< void(void)> Task
Handle for a single, executable task.
Definition: Task.h:60
size_t active() const
Returns the number of currently active/busy threads.
Definition: ThreadPool.h:531
volatile size_t active_
Number of currently active/busy threads.
Definition: ThreadPool.h:380
bool executeTask()
Executing a scheduled task.
Definition: ThreadPool.h:766
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:377
Task queue for the thread pool.
~ThreadPool()
Destructor for the ThreadPool class.
Definition: ThreadPool.h:451
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:385
bool isEmpty() const
Returns whether any tasks are scheduled for execution.
Definition: ThreadPool.h:497
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:107
MT Mutex
Type of the mutex.
Definition: ThreadPool.h:317
ThreadPool(size_t n)
Constructor for the ThreadPool class.
Definition: ThreadPool.h:418
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:381
threadpool::TaskQueue TaskQueue
Type of the task queue.
Definition: ThreadPool.h:316
Header file for the Task base class.
void wait()
Waiting for all scheduled tasks to be completed.
Definition: ThreadPool.h:695
volatile size_t total_
Total number of threads in the thread pool.
Definition: ThreadPool.h:376
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:548
TaskQueue taskqueue_
Task queue for the scheduled tasks.
Definition: ThreadPool.h:382
void schedule(Callable func, Args &&...args)
Scheduling the given function/functor for execution.
Definition: ThreadPool.h:581
#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