All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ThreadPool.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_UTIL_THREADPOOL_THREADPOOL_H_
36 #define _BLAZE_UTIL_THREADPOOL_THREADPOOL_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <boost/bind.hpp>
44 #include <boost/thread/condition.hpp>
45 #include <boost/thread/mutex.hpp>
46 #include <blaze/util/NonCopyable.h>
47 #include <blaze/util/PtrVector.h>
49 #include <blaze/util/Thread.h>
52 #include <blaze/util/Types.h>
53 
54 
55 namespace blaze {
56 
57 //=================================================================================================
58 //
59 // CLASS DEFINITION
60 //
61 //=================================================================================================
62 
63 //*************************************************************************************************
232 class ThreadPool : private NonCopyable
233 {
234  private:
235  //**Type definitions****************************************************************************
238  typedef boost::mutex Mutex;
239  typedef Mutex::scoped_lock Lock;
240  typedef boost::condition_variable Condition;
241  //**********************************************************************************************
242 
243  public:
244  //**Constructor*********************************************************************************
247  explicit ThreadPool( size_t n );
249  //**********************************************************************************************
250 
251  //**Destructor**********************************************************************************
254  ~ThreadPool();
256  //**********************************************************************************************
257 
258  //**Get functions*******************************************************************************
261  inline bool isEmpty() const;
262  inline size_t size() const;
263  inline size_t active() const;
264  inline size_t ready() const;
266  //**********************************************************************************************
267 
268  //**Scheduling functions************************************************************************
271  template< typename Callable >
272  void schedule( Callable func );
273 
274  template< typename Callable, typename A1 >
275  void schedule( Callable func, A1 a1 );
276 
277  template< typename Callable, typename A1, typename A2 >
278  void schedule( Callable func, A1 a1, A2 a2 );
279 
280  template< typename Callable, typename A1, typename A2, typename A3 >
281  void schedule( Callable func, A1 a1, A2 a2, A3 a3 );
282 
283  template< typename Callable, typename A1, typename A2, typename A3, typename A4 >
284  void schedule( Callable func, A1 a1, A2 a2, A3 a3, A4 a4 );
285 
286  template< typename Callable, typename A1, typename A2, typename A3, typename A4, typename A5 >
287  void schedule( Callable func, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5 );
289  //**********************************************************************************************
290 
291  //**Utility functions***************************************************************************
294  void resize( size_t n );
295  void wait();
296  void clear();
298  //**********************************************************************************************
299 
300  private:
301  //**Thread functions****************************************************************************
304  void createThread();
305  bool executeTask();
307  //**********************************************************************************************
308 
309  //**Member variables****************************************************************************
312  volatile size_t total_;
313  volatile size_t expected_;
314 
316  volatile size_t active_;
319  mutable Mutex mutex_;
322 
323  //**********************************************************************************************
324 
325  //**Friend declarations*************************************************************************
327  friend class Thread;
329  //**********************************************************************************************
330 };
331 //*************************************************************************************************
332 
333 
334 
335 
336 //=================================================================================================
337 //
338 // GET FUNCTIONS
339 //
340 //=================================================================================================
341 
342 //*************************************************************************************************
347 inline bool ThreadPool::isEmpty() const
348 {
349  Lock lock( mutex_ );
350  return taskqueue_.isEmpty();
351 }
352 //*************************************************************************************************
353 
354 
355 //*************************************************************************************************
360 inline size_t ThreadPool::size() const
361 {
362  Lock lock( mutex_ );
363  return expected_;
364 }
365 //*************************************************************************************************
366 
367 
368 //*************************************************************************************************
373 inline size_t ThreadPool::active() const
374 {
375  Lock lock( mutex_ );
376  return active_;
377 }
378 //*************************************************************************************************
379 
380 
381 //*************************************************************************************************
386 inline size_t ThreadPool::ready() const
387 {
388  Lock lock( mutex_ );
389  return expected_ - active_;
390 }
391 //*************************************************************************************************
392 
393 
394 
395 
396 //=================================================================================================
397 //
398 // SCHEDULING FUNCTIONS
399 //
400 //=================================================================================================
401 
402 //*************************************************************************************************
411 template< typename Callable > // Task type
412 void ThreadPool::schedule( Callable func )
413 {
414  Lock lock( mutex_ );
415  taskqueue_.push( func );
416  waitForTask_.notify_one();
417 }
418 //*************************************************************************************************
419 
420 
421 //*************************************************************************************************
431 template< typename Callable // Type of the function/functor
432  , typename A1 > // Type of the first argument
433 void ThreadPool::schedule( Callable func, A1 a1 )
434 {
435  Lock lock( mutex_ );
436  taskqueue_.push( boost::bind<void>( func, a1 ) );
437  waitForTask_.notify_one();
438 }
439 //*************************************************************************************************
440 
441 
442 //*************************************************************************************************
453 template< typename Callable // Type of the function/functor
454  , typename A1 // Type of the first argument
455  , typename A2 > // Type of the second argument
456 void ThreadPool::schedule( Callable func, A1 a1, A2 a2 )
457 {
458  Lock lock( mutex_ );
459  taskqueue_.push( boost::bind<void>( func, a1, a2 ) );
460  waitForTask_.notify_one();
461 }
462 //*************************************************************************************************
463 
464 
465 //*************************************************************************************************
477 template< typename Callable // Type of the function/functor
478  , typename A1 // Type of the first argument
479  , typename A2 // Type of the second argument
480  , typename A3 > // Type of the third argument
481 void ThreadPool::schedule( Callable func, A1 a1, A2 a2, A3 a3 )
482 {
483  Lock lock( mutex_ );
484  taskqueue_.push( boost::bind<void>( func, a1, a2, a3 ) );
485  waitForTask_.notify_one();
486 }
487 //*************************************************************************************************
488 
489 
490 //*************************************************************************************************
503 template< typename Callable // Type of the function/functor
504  , typename A1 // Type of the first argument
505  , typename A2 // Type of the second argument
506  , typename A3 // Type of the third argument
507  , typename A4 > // Type of the fourth argument
508 void ThreadPool::schedule( Callable func, A1 a1, A2 a2, A3 a3, A4 a4 )
509 {
510  Lock lock( mutex_ );
511  taskqueue_.push( boost::bind<void>( func, a1, a2, a3, a4 ) );
512  waitForTask_.notify_one();
513 }
514 //*************************************************************************************************
515 
516 
517 //*************************************************************************************************
531 template< typename Callable // Type of the function/functor
532  , typename A1 // Type of the first argument
533  , typename A2 // Type of the second argument
534  , typename A3 // Type of the third argument
535  , typename A4 // Type of the fourth argument
536  , typename A5 > // Type of the fifth argument
537 void ThreadPool::schedule( Callable func, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5 )
538 {
539  Lock lock( mutex_ );
540  taskqueue_.push( boost::bind<void>( func, a1, a2, a3, a4, a5 ) );
541  waitForTask_.notify_one();
542 }
543 //*************************************************************************************************
544 
545 } // namespace blaze
546 
547 #endif
ThreadPool(size_t n)
Constructor for the ThreadPool class.
Definition: ThreadPool.cpp:69
boost::condition_variable Condition
Condition variable type.
Definition: ThreadPool.h:240
void schedule(Callable func)
Scheduling the given zero argument function/functor for execution.
Definition: ThreadPool.h:412
Threads threads_
The threads contained in the thread pool.
Definition: ThreadPool.h:317
Base class for non-copyable class instances.
size_t size() const
Returns the current size of the thread pool.
Definition: ThreadPool.h:360
TaskQueue taskqueue_
Task queue for the scheduled tasks.
Definition: ThreadPool.h:318
volatile size_t expected_
Expected number of threads in the thread pool.
Definition: ThreadPool.h:313
Implementation of a vector for (polymorphic) pointers.
Task queue for the thread pool.
Condition waitForTask_
Wait condition for idle threads.
Definition: ThreadPool.h:320
Compile time assertion.
void createThread()
Adding a new thread to the thread pool.
Definition: ThreadPool.cpp:232
Header file for the Thread class.
void wait()
Waiting for all scheduled tasks to be completed.
Definition: ThreadPool.cpp:192
Base class for non-copyable class instances.The NonCopyable class is intended to work as a base class...
Definition: NonCopyable.h:63
size_t ready() const
Returns the number of currently ready/inactive threads.
Definition: ThreadPool.h:386
bool isEmpty() const
Returns whether any tasks are scheduled for execution.
Definition: ThreadPool.h:347
threadpool::TaskQueue TaskQueue
Type of the task queue.
Definition: ThreadPool.h:237
Implementation of a single thread of execution.
Definition: Thread.h:200
bool executeTask()
Executing a scheduled task.
Definition: ThreadPool.cpp:251
volatile size_t total_
Total number of threads in the thread pool.
Definition: ThreadPool.h:312
Mutex::scoped_lock Lock
Type of a locking object.
Definition: ThreadPool.h:239
volatile size_t active_
Number of currently active/busy threads.
Definition: ThreadPool.h:316
void resize(size_t n)
Changes the total number of threads in the thread pool.
Definition: ThreadPool.cpp:149
~ThreadPool()
Destructor for the ThreadPool class.
Definition: ThreadPool.cpp:98
Header file for the Task base class.
Condition waitForThread_
Wait condition for the thread management.
Definition: ThreadPool.h:321
void push(Task task)
Adding a task to the end of the task queue.
Definition: TaskQueue.h:225
Task queue for the thread pool.The TaskQueue class represents the internal task container of a thread...
Definition: TaskQueue.h:64
size_t active() const
Returns the number of currently active/busy threads.
Definition: ThreadPool.h:373
Header file for basic type definitions.
void clear()
Removing all scheduled tasks from the thread pool.
Definition: ThreadPool.cpp:211
PtrVector< Thread > Threads
Type of the thread container.
Definition: ThreadPool.h:236
bool isEmpty() const
Returns true if the task queue has no elements.
Definition: TaskQueue.h:202
Mutex mutex_
Synchronization mutex.
Definition: ThreadPool.h:319
Implementation of a thread pool.
Definition: ThreadPool.h:232
boost::mutex Mutex
Type of the mutex.
Definition: ThreadPool.h:238