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 <functional>
44 #include <memory>
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 std::unique_ptr<ThreadType> ThreadHandle;
257  //**********************************************************************************************
258 
259  //**Constructors********************************************************************************
262  explicit Thread( ThreadPoolType* pool );
264  //**********************************************************************************************
265 
266  public:
267  //**Constructors********************************************************************************
270  template< typename Callable, typename... Args >
271  explicit inline Thread( Callable func, Args&&... args );
273  //**********************************************************************************************
274 
275  //**Destructor**********************************************************************************
278  ~Thread();
280  //**********************************************************************************************
281 
282  //**Utility functions***************************************************************************
285  inline bool joinable() const;
286  inline void join();
288  //**********************************************************************************************
289 
290  private:
291  //**Utility functions***************************************************************************
294  inline bool hasTerminated() const;
296  //**********************************************************************************************
297 
298  //**Thread execution functions******************************************************************
301  void run();
303  //**********************************************************************************************
304 
305  //**Member variables****************************************************************************
308  volatile bool terminated_;
309 
312  ThreadPoolType* pool_;
313  ThreadHandle thread_;
314 
315  //**********************************************************************************************
316 
317  //**Friend declarations*************************************************************************
319  friend class ThreadPool<TT,MT,LT,CT>;
321  //**********************************************************************************************
322 };
323 //*************************************************************************************************
324 
325 
326 
327 
328 //=================================================================================================
329 //
330 // CONSTRUCTORS
331 //
332 //=================================================================================================
333 
334 //*************************************************************************************************
342 template< typename TT // Type of the encapsulated thread
343  , typename MT // Type of the synchronization mutex
344  , typename LT // Type of the mutex lock
345  , typename CT > // Type of the condition variable
347  : terminated_( false ) // Thread termination flag
348  , pool_ ( pool ) // Handle to the managing thread pool
349  , thread_ ( nullptr ) // Handle to the thread of execution
350 {
351  thread_.reset( new ThreadType( std::bind( &Thread::run, this ) ) );
352 }
353 //*************************************************************************************************
354 
355 
356 //*************************************************************************************************
365 template< typename TT // Type of the encapsulated thread
366  , typename MT // Type of the synchronization mutex
367  , typename LT // Type of the mutex lock
368  , typename CT > // Type of the condition variable
369 template< typename Callable // Type of the function/functor
370  , typename... Args > // Types of the function/functor arguments
371 inline Thread<TT,MT,LT,CT>::Thread( Callable func, Args&&... args )
372  : pool_ ( nullptr ) // Handle to the managing thread pool
373  , thread_( nullptr ) // Handle to the thread of execution
374 {
375  thread_.reset( new ThreadType( func, std::forward<Args>( args )... ) );
376 }
377 //*************************************************************************************************
378 
379 
380 
381 
382 //=================================================================================================
383 //
384 // DESTRUCTOR
385 //
386 //=================================================================================================
387 
388 //*************************************************************************************************
391 template< typename TT // Type of the encapsulated thread
392  , typename MT // Type of the synchronization mutex
393  , typename LT // Type of the mutex lock
394  , typename CT > // Type of the condition variable
396 {}
397 //*************************************************************************************************
398 
399 
400 
401 
402 //=================================================================================================
403 //
404 // UTILITY FUNCTIONS
405 //
406 //=================================================================================================
407 
408 //*************************************************************************************************
417 template< typename TT // Type of the encapsulated thread
418  , typename MT // Type of the synchronization mutex
419  , typename LT // Type of the mutex lock
420  , typename CT > // Type of the condition variable
421 inline bool Thread<TT,MT,LT,CT>::joinable() const
422 {
423  return thread_->joinable();
424 }
425 //*************************************************************************************************
426 
427 
428 //*************************************************************************************************
436 template< typename TT // Type of the encapsulated thread
437  , typename MT // Type of the synchronization mutex
438  , typename LT // Type of the mutex lock
439  , typename CT > // Type of the condition variable
441 {
442  thread_->join();
443 }
444 //*************************************************************************************************
445 
446 
447 //*************************************************************************************************
455 template< typename TT // Type of the encapsulated thread
456  , typename MT // Type of the synchronization mutex
457  , typename LT // Type of the mutex lock
458  , typename CT > // Type of the condition variable
460 {
461  return terminated_;
462 }
463 //*************************************************************************************************
464 
465 
466 
467 
468 //=================================================================================================
469 //
470 // THREAD EXECUTION FUNCTIONS
471 //
472 //=================================================================================================
473 
474 //*************************************************************************************************
479 template< typename TT // Type of the encapsulated thread
480  , typename MT // Type of the synchronization mutex
481  , typename LT // Type of the mutex lock
482  , typename CT > // Type of the condition variable
484 {
485  // Checking the thread pool handle
486  BLAZE_INTERNAL_ASSERT( pool_, "Uninitialized pool handle detected" );
487 
488  // Executing scheduled tasks
489  while( pool_->executeTask() ) {}
490 
491  // Setting the termination flag
492  terminated_ = true;
493 }
494 //*************************************************************************************************
495 
496 } // namespace blaze
497 
498 #endif
bool hasTerminated() const
Returns whether the thread has terminated its execution.
Definition: Thread.h:459
Thread(ThreadPoolType *pool)
Starting a thread in a thread pool.
Definition: Thread.h:346
Base class for non-copyable class instances.
volatile bool terminated_
Thread termination flag.
Definition: Thread.h:308
bool joinable() const
Returns whether this is a thread of execution.
Definition: Thread.h:421
ThreadHandle thread_
Handle to the thread of execution.
Definition: Thread.h:313
void join()
Waiting for a thread of execution to complete.
Definition: Thread.h:440
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
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:483
Implementation of a single thread of execution.
Definition: Thread.h:250
std::unique_ptr< ThreadType > ThreadHandle
Handle for a single thread.
Definition: Thread.h:256
Header file for run time assertion macros.
ThreadPoolType * pool_
Handle to the managing thread pool.
Definition: Thread.h:312
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:395