35 #ifndef _BLAZE_UTIL_THREADPOOL_THREADPOOL_H_ 36 #define _BLAZE_UTIL_THREADPOOL_THREADPOOL_H_ 307 template<
typename TT
312 :
private NonCopyable
320 using Threads = std::vector< std::unique_ptr<ManagedThread> >;
347 inline size_t size()
const;
348 inline size_t active()
const;
349 inline size_t ready()
const;
356 template<
typename Callable,
typename... Args >
357 void schedule( Callable func, Args&&... args );
364 void resize(
size_t n,
bool block=
false );
397 friend class Thread<TT,MT,LT,CT>;
420 template<
typename TT
453 template<
typename TT
468 waitForTask_.notify_all();
471 while( total_ != 0UL ) {
472 waitForThread_.wait( lock );
476 for(
auto const& thread : threads_ ) {
499 template<
typename TT
506 return taskqueue_.isEmpty();
516 template<
typename TT
533 template<
typename TT
550 template<
typename TT
557 return expected_ - active_;
581 template<
typename TT
585 template<
typename Callable
590 taskqueue_.push( std::bind<void>( func, std::forward<Args>( args )... ) );
591 waitForTask_.notify_one();
644 template<
typename TT
651 #if !(defined _MSC_VER) 662 if( n > expected_ ) {
663 for(
size_t i=expected_; i<n; ++i )
670 waitForTask_.notify_all();
672 while( block && total_ != expected_ ) {
673 waitForThread_.wait( lock );
678 for(
typename Threads::iterator thread=threads_.begin(); thread!=threads_.end(); ) {
679 if( (*thread)->hasTerminated() ) {
681 thread = threads_.erase( thread );
697 template<
typename TT
705 while( !taskqueue_.isEmpty() || active_ > 0UL ) {
706 waitForThread_.wait( lock );
720 template<
typename TT
745 template<
typename TT
751 threads_.push_back( std::unique_ptr<ManagedThread>(
new ManagedThread(
this ) ) );
768 template<
typename TT
780 while( taskqueue_.isEmpty() )
783 waitForThread_.notify_all();
785 if( total_ > expected_ ) {
790 waitForTask_.wait( lock );
795 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
Header file for basic type definitions.
Condition waitForTask_
Wait condition for idle threads.
Definition: ThreadPool.h:390
void clear()
Removing all scheduled tasks from the thread pool.
Definition: ThreadPool.h:724
CT Condition
Condition variable type.
Definition: ThreadPool.h:325
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:749
size_t size() const
Returns the current size of the thread pool.
Definition: ThreadPool.h:520
Mutex mutex_
Synchronization mutex.
Definition: ThreadPool.h:389
void resize(size_t n, bool block=false)
Changes the total number of threads in the thread pool.
Definition: ThreadPool.h:648
std::vector< std::unique_ptr< ManagedThread > > Threads
Type of the thread container.
Definition: ThreadPool.h:320
volatile size_t active_
Number of currently active/busy threads.
Definition: ThreadPool.h:386
bool executeTask()
Executing a scheduled task.
Definition: ThreadPool.h:772
volatile size_t expected_
Expected number of threads in the thread pool.
Definition: ThreadPool.h:383
Task queue for the thread pool.
~ThreadPool()
Destructor for the ThreadPool class.
Definition: ThreadPool.h:457
std::function< void(void)> Task
Handle for a single, executable task.
Definition: Task.h:60
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
Header file for the Thread class.
Condition waitForThread_
Wait condition for the thread management.
Definition: ThreadPool.h:391
Implementation of a single thread of execution.
Definition: Thread.h:250
size_t active() const
Returns the number of currently active/busy threads.
Definition: ThreadPool.h:537
ThreadPool(size_t n)
Constructor for the ThreadPool class.
Definition: ThreadPool.h:424
size_t ready() const
Returns the number of currently ready/inactive threads.
Definition: ThreadPool.h:554
bool isEmpty() const
Returns whether any tasks are scheduled for execution.
Definition: ThreadPool.h:503
void schedule(Callable func, Args &&... args)
Scheduling the given function/functor for execution.
Definition: ThreadPool.h:587
Header file for run time assertion macros.
Threads threads_
The threads contained in the thread pool.
Definition: ThreadPool.h:387
Header file for the Task base class.
void wait()
Waiting for all scheduled tasks to be completed.
Definition: ThreadPool.h:701
volatile size_t total_
Total number of threads in the thread pool.
Definition: ThreadPool.h:382
Task queue for the thread pool.The TaskQueue class represents the internal task container of a thread...
Definition: TaskQueue.h:64
TaskQueue taskqueue_
Task queue for the scheduled tasks.
Definition: ThreadPool.h:388
MT Mutex
Type of the mutex.
Definition: ThreadPool.h:323
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.In case of an invalid run time expression,...
Definition: Assert.h:101
LT Lock
Type of a locking object.
Definition: ThreadPool.h:324