Blaze 3.9
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>
47
48
49namespace blaze {
50
51//=================================================================================================
52//
53// ::blaze NAMESPACE FORWARD DECLARATIONS
54//
55//=================================================================================================
56
57template< typename TT, typename MT, typename LT, typename CT > class ThreadPool;
58
59
60
61
62//=================================================================================================
63//
64// CLASS DEFINITION
65//
66//=================================================================================================
67
68//*************************************************************************************************
246template< 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
251 : private NonCopyable
252{
253 private:
254 //**Type definitions****************************************************************************
255 using ThreadType = TT;
257 using ThreadHandle = std::unique_ptr<ThreadType>;
258 //**********************************************************************************************
259
260 //**Constructors********************************************************************************
263 explicit Thread( ThreadPoolType* pool );
265 //**********************************************************************************************
266
267 public:
268 //**Constructors********************************************************************************
271 template< typename Callable, typename... Args >
272 explicit inline Thread( Callable func, Args&&... args );
274 //**********************************************************************************************
275
276 //**Destructor**********************************************************************************
279 ~Thread();
281 //**********************************************************************************************
282
283 //**Utility functions***************************************************************************
286 inline bool joinable() const;
287 inline void join();
289 //**********************************************************************************************
290
291 private:
292 //**Utility functions***************************************************************************
295 inline bool hasTerminated() const;
297 //**********************************************************************************************
298
299 //**Thread execution functions******************************************************************
302 void run();
304 //**********************************************************************************************
305
306 //**Member variables****************************************************************************
309 volatile bool terminated_;
316 //**********************************************************************************************
317
318 //**Friend declarations*************************************************************************
320 friend class ThreadPool<TT,MT,LT,CT>;
322 //**********************************************************************************************
323};
324//*************************************************************************************************
325
326
327
328
329//=================================================================================================
330//
331// CONSTRUCTORS
332//
333//=================================================================================================
334
335//*************************************************************************************************
343template< typename TT // Type of the encapsulated thread
344 , typename MT // Type of the synchronization mutex
345 , typename LT // Type of the mutex lock
346 , typename CT > // Type of the condition variable
348 : terminated_( false ) // Thread termination flag
349 , pool_ ( pool ) // Handle to the managing thread pool
350 , thread_ ( nullptr ) // Handle to the thread of execution
351{
352 thread_.reset( new ThreadType( std::bind( &Thread::run, this ) ) );
353}
354//*************************************************************************************************
355
356
357//*************************************************************************************************
366template< typename TT // Type of the encapsulated thread
367 , typename MT // Type of the synchronization mutex
368 , typename LT // Type of the mutex lock
369 , typename CT > // Type of the condition variable
370template< typename Callable // Type of the function/functor
371 , typename... Args > // Types of the function/functor arguments
372inline Thread<TT,MT,LT,CT>::Thread( Callable func, Args&&... args )
373 : pool_ ( nullptr ) // Handle to the managing thread pool
374 , thread_( nullptr ) // Handle to the thread of execution
375{
376 thread_.reset( new ThreadType( func, std::forward<Args>( args )... ) );
377}
378//*************************************************************************************************
379
380
381
382
383//=================================================================================================
384//
385// DESTRUCTOR
386//
387//=================================================================================================
388
389//*************************************************************************************************
392template< typename TT // Type of the encapsulated thread
393 , typename MT // Type of the synchronization mutex
394 , typename LT // Type of the mutex lock
395 , typename CT > // Type of the condition variable
397{}
398//*************************************************************************************************
399
400
401
402
403//=================================================================================================
404//
405// UTILITY FUNCTIONS
406//
407//=================================================================================================
408
409//*************************************************************************************************
418template< typename TT // Type of the encapsulated thread
419 , typename MT // Type of the synchronization mutex
420 , typename LT // Type of the mutex lock
421 , typename CT > // Type of the condition variable
423{
424 return thread_->joinable();
425}
426//*************************************************************************************************
427
428
429//*************************************************************************************************
437template< typename TT // Type of the encapsulated thread
438 , typename MT // Type of the synchronization mutex
439 , typename LT // Type of the mutex lock
440 , typename CT > // Type of the condition variable
442{
443 thread_->join();
444}
445//*************************************************************************************************
446
447
448//*************************************************************************************************
456template< typename TT // Type of the encapsulated thread
457 , typename MT // Type of the synchronization mutex
458 , typename LT // Type of the mutex lock
459 , typename CT > // Type of the condition variable
461{
462 return terminated_;
463}
464//*************************************************************************************************
465
466
467
468
469//=================================================================================================
470//
471// THREAD EXECUTION FUNCTIONS
472//
473//=================================================================================================
474
475//*************************************************************************************************
480template< typename TT // Type of the encapsulated thread
481 , typename MT // Type of the synchronization mutex
482 , typename LT // Type of the mutex lock
483 , typename CT > // Type of the condition variable
485{
486 // Checking the thread pool handle
487 BLAZE_INTERNAL_ASSERT( pool_, "Uninitialized pool handle detected" );
488
489 // Executing scheduled tasks
490 while( pool_->executeTask() ) {}
491
492 // Setting the termination flag
493 terminated_ = true;
494}
495//*************************************************************************************************
496
497} // namespace blaze
498
499#endif
Header file for run time assertion macros.
Base class for non-copyable class instances.
Base class for non-copyable class instances.
Definition: NonCopyable.h:64
Implementation of a thread pool.
Definition: ThreadPool.h:313
Implementation of a single thread of execution.
Definition: Thread.h:252
ThreadPoolType * pool_
Handle to the managing thread pool.
Definition: Thread.h:313
void join()
Waiting for a thread of execution to complete.
Definition: Thread.h:441
void run()
Execution function for threads in a thread pool.
Definition: Thread.h:484
ThreadHandle thread_
Handle to the thread of execution.
Definition: Thread.h:314
std::unique_ptr< ThreadType > ThreadHandle
Handle for a single thread.
Definition: Thread.h:257
~Thread()
Destructor for the Thread class.
Definition: Thread.h:396
bool hasTerminated() const
Returns whether the thread has terminated its execution.
Definition: Thread.h:460
Thread(ThreadPoolType *pool)
Starting a thread in a thread pool.
Definition: Thread.h:347
bool joinable() const
Returns whether this is a thread of execution.
Definition: Thread.h:422
TT ThreadType
Type of the encapsulated thread.
Definition: Thread.h:255
volatile bool terminated_
Thread termination flag.
Definition: Thread.h:309
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.
Definition: Assert.h:101