Blaze
3.9
Toggle main menu visibility
Loading...
Searching...
No Matches
blaze
util
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
251
:
private
NonCopyable
252
{
253
private
:
254
//**Type definitions****************************************************************************
255
using
ThreadType
= TT;
256
using
ThreadPoolType
=
ThreadPool<TT,MT,LT,CT>
;
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_
;
313
ThreadPoolType
*
pool_
;
314
ThreadHandle
thread_
;
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
//*************************************************************************************************
343
template
<
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
347
Thread<TT,MT,LT,CT>::Thread
(
ThreadPoolType
* pool )
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
//*************************************************************************************************
366
template
<
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
370
template
<
typename
Callable
// Type of the function/functor
371
,
typename
... Args >
// Types of the function/functor arguments
372
inline
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
//*************************************************************************************************
392
template
<
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
396
Thread<TT,MT,LT,CT>::~Thread
()
397
{}
398
//*************************************************************************************************
399
400
401
402
403
//=================================================================================================
404
//
405
// UTILITY FUNCTIONS
406
//
407
//=================================================================================================
408
409
//*************************************************************************************************
418
template
<
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
422
inline
bool
Thread<TT,MT,LT,CT>::joinable
()
const
423
{
424
return
thread_
->joinable();
425
}
426
//*************************************************************************************************
427
428
429
//*************************************************************************************************
437
template
<
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
441
inline
void
Thread<TT,MT,LT,CT>::join
()
442
{
443
thread_
->join();
444
}
445
//*************************************************************************************************
446
447
448
//*************************************************************************************************
456
template
<
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
460
inline
bool
Thread<TT,MT,LT,CT>::hasTerminated
()
const
461
{
462
return
terminated_
;
463
}
464
//*************************************************************************************************
465
466
467
468
469
//=================================================================================================
470
//
471
// THREAD EXECUTION FUNCTIONS
472
//
473
//=================================================================================================
474
475
//*************************************************************************************************
480
template
<
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
484
void
Thread<TT,MT,LT,CT>::run
()
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
Assert.h
Header file for run time assertion macros.
NonCopyable.h
Base class for non-copyable class instances.
blaze::NonCopyable::NonCopyable
NonCopyable()
Default constructor for the NonCopyable class.
Definition
NonCopyable.h:69
blaze::ThreadPool
Implementation of a thread pool.
Definition
ThreadPool.h:313
blaze::Thread::pool_
ThreadPoolType * pool_
Handle to the managing thread pool.
Definition
Thread.h:313
blaze::Thread::join
void join()
Waiting for a thread of execution to complete.
Definition
Thread.h:441
blaze::Thread::run
void run()
Execution function for threads in a thread pool.
Definition
Thread.h:484
blaze::Thread::ThreadPoolType
ThreadPool< TT, MT, LT, CT > ThreadPoolType
Type of the managing thread pool.
Definition
Thread.h:256
blaze::Thread::thread_
ThreadHandle thread_
Handle to the thread of execution.
Definition
Thread.h:314
blaze::Thread::ThreadHandle
std::unique_ptr< ThreadType > ThreadHandle
Handle for a single thread.
Definition
Thread.h:257
blaze::Thread::~Thread
~Thread()
Destructor for the Thread class.
Definition
Thread.h:396
blaze::Thread::hasTerminated
bool hasTerminated() const
Returns whether the thread has terminated its execution.
Definition
Thread.h:460
blaze::Thread::Thread
Thread(ThreadPoolType *pool)
Starting a thread in a thread pool.
Definition
Thread.h:347
blaze::Thread::joinable
bool joinable() const
Returns whether this is a thread of execution.
Definition
Thread.h:422
blaze::Thread::ThreadType
TT ThreadType
Type of the encapsulated thread.
Definition
Thread.h:255
blaze::Thread::terminated_
volatile bool terminated_
Thread termination flag.
Definition
Thread.h:309
BLAZE_INTERNAL_ASSERT
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.
Definition
Assert.h:101
Generated by
1.18.0