Logo ROOT  
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
17namespace ROOT {
18
19 /**
20 * \class ROOT::TSpinMutex
21 * \brief A spin mutex class which respects the STL interface for mutexes.
22 * \ingroup Multicore
23 * This class allows to acquire spin locks also in combination with templates in the STL such as
24 * <a href="http://en.cppreference.com/w/cpp/thread/unique_lock">std::unique_lock</a> or
25 * <a href="http://en.cppreference.com/w/cpp/thread/condition_variable_any">std::condition_variable_any</a>.
26 * For example:
27 *
28 * ~~~ {.cpp}
29 * ROOT::TSpinMutex m;
30 * std::condition_variable cv;
31 * bool ready = false;
32 *
33 * void worker_thread()
34 * {
35 * // Wait until main() sends data
36 * std::unique_lock<ROOT::TSpinMutex> lk(m);
37 * cv.wait(lk, []{return ready;});
38 * [...]
39 * }
40 * ~~~ {.cpp}
41 */
42 class TSpinMutex {
43
44 private:
45 std::atomic_flag fAFlag = ATOMIC_FLAG_INIT;
46
47 public:
48 TSpinMutex() = default;
49 TSpinMutex(const TSpinMutex&) = delete;
50 ~TSpinMutex() = default;
51 TSpinMutex& operator=(const TSpinMutex&) = delete;
52
53 void lock() { while (fAFlag.test_and_set(std::memory_order_acquire)); }
54 void unlock() { fAFlag.clear(std::memory_order_release); }
55 bool try_lock() { return !fAFlag.test_and_set(std::memory_order_acquire); }
56
57 };
58}
59
60#endif
A spin mutex class which respects the STL interface for mutexes.
TSpinMutex()=default
TSpinMutex(const TSpinMutex &)=delete
std::atomic_flag fAFlag
~TSpinMutex()=default
TSpinMutex & operator=(const TSpinMutex &)=delete
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...