ROOT
master
Reference Guide
Loading...
Searching...
No Matches
TSpinMutex.hxx
Go to the documentation of this file.
1
// @(#)root/thread
2
// Author: Danilo Piparo, 2016
3
4
/*************************************************************************
5
* Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6
* All rights reserved. *
7
* *
8
* For the licensing terms see $ROOTSYS/LICENSE. *
9
* For the list of contributors see $ROOTSYS/README/CREDITS. *
10
*************************************************************************/
11
12
#ifndef ROOT_TSpinMutex
13
#define ROOT_TSpinMutex
14
15
#include <atomic>
16
17
namespace
ROOT
{
18
19
/**
20
* \class ROOT::TSpinMutex
21
* \brief A spin mutex class which respects the STL interface for mutexes.
22
* This class allows to acquire spin locks also in combination with templates in the STL such as
23
* <a href="http://en.cppreference.com/w/cpp/thread/unique_lock">std::unique_lock</a> or
24
* <a href="http://en.cppreference.com/w/cpp/thread/condition_variable_any">std::condition_variable_any</a>.
25
* For example:
26
*
27
* ~~~ {.cpp}
28
* ROOT::TSpinMutex m;
29
* std::condition_variable cv;
30
* bool ready = false;
31
*
32
* void worker_thread()
33
* {
34
* // Wait until main() sends data
35
* std::unique_lock<ROOT::TSpinMutex> lk(m);
36
* cv.wait(lk, []{return ready;});
37
* [...]
38
* }
39
* ~~~ {.cpp}
40
*/
41
class
TSpinMutex
{
42
43
private
:
44
std::atomic_flag
fAFlag
=
ATOMIC_FLAG_INIT
;
45
46
public
:
47
TSpinMutex
() =
default
;
48
TSpinMutex
(
const
TSpinMutex
&) =
delete
;
49
~TSpinMutex
() =
default
;
50
TSpinMutex
&
operator=
(
const
TSpinMutex
&) =
delete
;
51
52
void
lock
() {
while
(
fAFlag
.test_and_set(std::memory_order_acquire)); }
53
void
unlock
() {
fAFlag
.clear(std::memory_order_release); }
54
bool
try_lock
() {
return
!
fAFlag
.test_and_set(std::memory_order_acquire); }
55
56
};
57
}
58
59
#endif
TRangeDynCast
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Definition
TCollection.h:360
ROOT::TSpinMutex
A spin mutex class which respects the STL interface for mutexes.
Definition
TSpinMutex.hxx:41
ROOT::TSpinMutex::TSpinMutex
TSpinMutex()=default
ROOT::TSpinMutex::try_lock
bool try_lock()
Definition
TSpinMutex.hxx:54
ROOT::TSpinMutex::lock
void lock()
Definition
TSpinMutex.hxx:52
ROOT::TSpinMutex::unlock
void unlock()
Definition
TSpinMutex.hxx:53
ROOT::TSpinMutex::TSpinMutex
TSpinMutex(const TSpinMutex &)=delete
ROOT::TSpinMutex::fAFlag
std::atomic_flag fAFlag
Definition
TSpinMutex.hxx:44
ROOT::TSpinMutex::~TSpinMutex
~TSpinMutex()=default
ROOT::TSpinMutex::operator=
TSpinMutex & operator=(const TSpinMutex &)=delete
ROOT
Definition
EExecutionPolicy.hxx:4
core
thread
inc
ROOT
TSpinMutex.hxx
ROOTmaster - Reference Guide Generated on Wed May 13 2026 15:40:57 (GVA Time) using Doxygen 1.10.0