All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Thread.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_UTIL_THREAD_H_
36 #define _BLAZE_UTIL_THREAD_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <boost/bind.hpp>
44 #include <boost/scoped_ptr.hpp>
45 #include <blaze/util/Assert.h>
46 #include <blaze/util/NonCopyable.h>
47 
48 
49 namespace blaze {
50 
51 //=================================================================================================
52 //
53 // ::blaze NAMESPACE FORWARD DECLARATIONS
54 //
55 //=================================================================================================
56 
57 template< typename TT, typename MT, typename LT, typename CT > class ThreadPool;
58 
59 
60 
61 
62 //=================================================================================================
63 //
64 // CLASS DEFINITION
65 //
66 //=================================================================================================
67 
68 //*************************************************************************************************
246 template< typename TT // Type of the encapsulated thread
247  , typename MT // Type of the synchronization mutex
248  , typename LT // Type of the mutex lock
249  , typename CT > // Type of the condition variable
250 class Thread : private NonCopyable
251 {
252  private:
253  //**Type definitions****************************************************************************
254  typedef TT ThreadType;
256  typedef boost::scoped_ptr<ThreadType> ThreadHandle;
257  //**********************************************************************************************
258 
259  //**Constructors********************************************************************************
262  explicit Thread( ThreadPoolType* pool );
264  //**********************************************************************************************
265 
266  public:
267  //**Constructors********************************************************************************
270  template< typename Callable >
271  explicit inline Thread( Callable func );
272 
273  template< typename Callable, typename A1 >
274  explicit inline Thread( Callable func, A1 a1 );
275 
276  template< typename Callable, typename A1, typename A2 >
277  explicit inline Thread( Callable func, A1 a1, A2 a2 );
278 
279  template< typename Callable, typename A1, typename A2, typename A3 >
280  explicit inline Thread( Callable func, A1 a1, A2 a2, A3 a3 );
281 
282  template< typename Callable, typename A1, typename A2, typename A3, typename A4 >
283  explicit inline Thread( Callable func, A1 a1, A2 a2, A3 a3, A4 a4 );
284 
285  template< typename Callable, typename A1, typename A2, typename A3, typename A4, typename A5 >
286  explicit inline Thread( Callable func, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5 );
288  //**********************************************************************************************
289 
290  //**Destructor**********************************************************************************
293  ~Thread();
295  //**********************************************************************************************
296 
297  //**Utility functions***************************************************************************
300  inline bool joinable() const;
301  inline void join();
303  //**********************************************************************************************
304 
305  private:
306  //**Utility functions***************************************************************************
309  inline bool hasTerminated() const;
311  //**********************************************************************************************
312 
313  //**Thread execution functions******************************************************************
316  void run();
318  //**********************************************************************************************
319 
320  //**Member variables****************************************************************************
323  volatile bool terminated_;
324 
329 
330  //**********************************************************************************************
331 
332  //**Friend declarations*************************************************************************
334  friend class ThreadPool<TT,MT,LT,CT>;
336  //**********************************************************************************************
337 };
338 //*************************************************************************************************
339 
340 
341 
342 
343 //=================================================================================================
344 //
345 // CONSTRUCTORS
346 //
347 //=================================================================================================
348 
349 //*************************************************************************************************
357 template< typename TT // Type of the encapsulated thread
358  , typename MT // Type of the synchronization mutex
359  , typename LT // Type of the mutex lock
360  , typename CT > // Type of the condition variable
362  : terminated_( false ) // Thread termination flag
363  , pool_ ( pool ) // Handle to the managing thread pool
364  , thread_ ( 0 ) // Handle to the thread of execution
365 {
366  thread_.reset( new ThreadType( boost::bind( &Thread::run, this ) ) );
367 }
368 //*************************************************************************************************
369 
370 
371 //*************************************************************************************************
379 template< typename TT // Type of the encapsulated thread
380  , typename MT // Type of the synchronization mutex
381  , typename LT // Type of the mutex lock
382  , typename CT > // Type of the condition variable
383 template< typename Callable > // Type of the function/functor
384 inline Thread<TT,MT,LT,CT>::Thread( Callable func )
385  : pool_ ( 0 ) // Handle to the managing thread pool
386  , thread_( 0 ) // Handle to the thread of execution
387 {
388  thread_.reset( new ThreadType( func ) );
389 }
390 //*************************************************************************************************
391 
392 
393 //*************************************************************************************************
403 template< typename TT // Type of the encapsulated thread
404  , typename MT // Type of the synchronization mutex
405  , typename LT // Type of the mutex lock
406  , typename CT > // Type of the condition variable
407 template< typename Callable // Type of the function/functor
408  , typename A1 > // Type of the first argument
409 inline Thread<TT,MT,LT,CT>::Thread( Callable func, A1 a1 )
410  : pool_ ( 0 ) // Handle to the managing thread pool
411  , thread_( 0 ) // Handle to the thread of execution
412 {
413  thread_.reset( new ThreadType( func, a1 ) );
414 }
415 //*************************************************************************************************
416 
417 
418 //*************************************************************************************************
429 template< typename TT // Type of the encapsulated thread
430  , typename MT // Type of the synchronization mutex
431  , typename LT // Type of the mutex lock
432  , typename CT > // Type of the condition variable
433 template< typename Callable // Type of the function/functor
434  , typename A1 // Type of the first argument
435  , typename A2 > // Type of the second argument
436 inline Thread<TT,MT,LT,CT>::Thread( Callable func, A1 a1, A2 a2 )
437  : pool_ ( 0 ) // Handle to the managing thread pool
438  , thread_( 0 ) // Handle to the thread of execution
439 {
440  thread_.reset( new ThreadType( func, a1, a2 ) );
441 }
442 //*************************************************************************************************
443 
444 
445 //*************************************************************************************************
457 template< typename TT // Type of the encapsulated thread
458  , typename MT // Type of the synchronization mutex
459  , typename LT // Type of the mutex lock
460  , typename CT > // Type of the condition variable
461 template< typename Callable // Type of the function/functor
462  , typename A1 // Type of the first argument
463  , typename A2 // Type of the second argument
464  , typename A3 > // Type of the third argument
465 inline Thread<TT,MT,LT,CT>::Thread( Callable func, A1 a1, A2 a2, A3 a3 )
466  : pool_ ( 0 ) // Handle to the managing thread pool
467  , thread_( 0 ) // Handle to the thread of execution
468 {
469  thread_.reset( new ThreadType( func, a1, a2, a3 ) );
470 }
471 //*************************************************************************************************
472 
473 
474 //*************************************************************************************************
487 template< typename TT // Type of the encapsulated thread
488  , typename MT // Type of the synchronization mutex
489  , typename LT // Type of the mutex lock
490  , typename CT > // Type of the condition variable
491 template< typename Callable // Type of the function/functor
492  , typename A1 // Type of the first argument
493  , typename A2 // Type of the second argument
494  , typename A3 // Type of the third argument
495  , typename A4 > // Type of the fourth argument
496 inline Thread<TT,MT,LT,CT>::Thread( Callable func, A1 a1, A2 a2, A3 a3, A4 a4 )
497  : pool_ ( 0 ) // Handle to the managing thread pool
498  , thread_( 0 ) // Handle to the thread of execution
499 {
500  thread_.reset( new ThreadType( func, a1, a2, a3, a4 ) );
501 }
502 //*************************************************************************************************
503 
504 
505 //*************************************************************************************************
519 template< typename TT // Type of the encapsulated thread
520  , typename MT // Type of the synchronization mutex
521  , typename LT // Type of the mutex lock
522  , typename CT > // Type of the condition variable
523 template< typename Callable // Type of the function/functor
524  , typename A1 // Type of the first argument
525  , typename A2 // Type of the second argument
526  , typename A3 // Type of the third argument
527  , typename A4 // Type of the fourth argument
528  , typename A5 > // Type of the fifth argument
529 inline Thread<TT,MT,LT,CT>::Thread( Callable func, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5 )
530  : pool_ ( 0 ) // Handle to the managing thread pool
531  , thread_( 0 ) // Handle to the thread of execution
532 {
533  thread_.reset( new ThreadType( func, a1, a2, a3, a4, a5 ) );
534 }
535 //*************************************************************************************************
536 
537 
538 
539 
540 //=================================================================================================
541 //
542 // DESTRUCTOR
543 //
544 //=================================================================================================
545 
546 //*************************************************************************************************
549 template< typename TT // Type of the encapsulated thread
550  , typename MT // Type of the synchronization mutex
551  , typename LT // Type of the mutex lock
552  , typename CT > // Type of the condition variable
554 {}
555 //*************************************************************************************************
556 
557 
558 
559 
560 //=================================================================================================
561 //
562 // UTILITY FUNCTIONS
563 //
564 //=================================================================================================
565 
566 //*************************************************************************************************
575 template< typename TT // Type of the encapsulated thread
576  , typename MT // Type of the synchronization mutex
577  , typename LT // Type of the mutex lock
578  , typename CT > // Type of the condition variable
579 inline bool Thread<TT,MT,LT,CT>::joinable() const
580 {
581  return thread_->joinable();
582 }
583 //*************************************************************************************************
584 
585 
586 //*************************************************************************************************
594 template< typename TT // Type of the encapsulated thread
595  , typename MT // Type of the synchronization mutex
596  , typename LT // Type of the mutex lock
597  , typename CT > // Type of the condition variable
599 {
600  thread_->join();
601 }
602 //*************************************************************************************************
603 
604 
605 //*************************************************************************************************
613 template< typename TT // Type of the encapsulated thread
614  , typename MT // Type of the synchronization mutex
615  , typename LT // Type of the mutex lock
616  , typename CT > // Type of the condition variable
618 {
619  return terminated_;
620 }
621 //*************************************************************************************************
622 
623 
624 
625 
626 //=================================================================================================
627 //
628 // THREAD EXECUTION FUNCTIONS
629 //
630 //=================================================================================================
631 
632 //*************************************************************************************************
637 template< typename TT // Type of the encapsulated thread
638  , typename MT // Type of the synchronization mutex
639  , typename LT // Type of the mutex lock
640  , typename CT > // Type of the condition variable
642 {
643  // Checking the thread pool handle
644  BLAZE_INTERNAL_ASSERT( pool_, "Uninitialized pool handle detected" );
645 
646  // Executing scheduled tasks
647  while( pool_->executeTask() ) {}
648 
649  // Setting the termination flag
650  terminated_ = true;
651 }
652 //*************************************************************************************************
653 
654 } // namespace blaze
655 
656 #endif
bool hasTerminated() const
Returns whether the thread has terminated its execution.
Definition: Thread.h:617
Thread(ThreadPoolType *pool)
Starting a thread in a thread pool.
Definition: Thread.h:361
Base class for non-copyable class instances.
volatile bool terminated_
Thread termination flag.
Definition: Thread.h:323
bool joinable() const
Returns whether this is a thread of execution.
Definition: Thread.h:579
ThreadHandle thread_
Handle to the thread of execution.
Definition: Thread.h:328
void join()
Waiting for a thread of execution to complete.
Definition: Thread.h:598
Base class for non-copyable class instances.The NonCopyable class is intended to work as a base class...
Definition: NonCopyable.h:63
void run()
Execution function for threads in a thread pool.
Definition: Thread.h:641
Implementation of a single thread of execution.
Definition: Thread.h:250
Header file for run time assertion macros.
boost::scoped_ptr< ThreadType > ThreadHandle
Handle for a single thread.
Definition: Thread.h:256
ThreadPoolType * pool_
Handle to the managing thread pool.
Definition: Thread.h:327
ThreadPool< TT, MT, LT, CT > ThreadPoolType
Type of the managing thread pool.
Definition: Thread.h:255
TT ThreadType
Type of the encapsulated thread.
Definition: Thread.h:254
Implementation of a thread pool.
Definition: Thread.h:57
#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
~Thread()
Destructor for the Thread class.
Definition: Thread.h:553