|
||||
File indexing completed on 2025-01-18 10:10:51
0001 // @(#)root/thread 0002 // Author: Danilo Piparo, 2016 0003 0004 /************************************************************************* 0005 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. * 0006 * All rights reserved. * 0007 * * 0008 * For the licensing terms see $ROOTSYS/LICENSE. * 0009 * For the list of contributors see $ROOTSYS/README/CREDITS. * 0010 *************************************************************************/ 0011 0012 #ifndef ROOT_TSpinMutex 0013 #define ROOT_TSpinMutex 0014 0015 #include <atomic> 0016 0017 namespace ROOT { 0018 0019 /** 0020 * \class ROOT::TSpinMutex 0021 * \brief A spin mutex class which respects the STL interface for mutexes. 0022 * \ingroup Parallelism 0023 * This class allows to acquire spin locks also in combination with templates in the STL such as 0024 * <a href="http://en.cppreference.com/w/cpp/thread/unique_lock">std::unique_lock</a> or 0025 * <a href="http://en.cppreference.com/w/cpp/thread/condition_variable_any">std::condition_variable_any</a>. 0026 * For example: 0027 * 0028 * ~~~ {.cpp} 0029 * ROOT::TSpinMutex m; 0030 * std::condition_variable cv; 0031 * bool ready = false; 0032 * 0033 * void worker_thread() 0034 * { 0035 * // Wait until main() sends data 0036 * std::unique_lock<ROOT::TSpinMutex> lk(m); 0037 * cv.wait(lk, []{return ready;}); 0038 * [...] 0039 * } 0040 * ~~~ {.cpp} 0041 */ 0042 class TSpinMutex { 0043 0044 private: 0045 std::atomic_flag fAFlag = ATOMIC_FLAG_INIT; 0046 0047 public: 0048 TSpinMutex() = default; 0049 TSpinMutex(const TSpinMutex&) = delete; 0050 ~TSpinMutex() = default; 0051 TSpinMutex& operator=(const TSpinMutex&) = delete; 0052 0053 void lock() { while (fAFlag.test_and_set(std::memory_order_acquire)); } 0054 void unlock() { fAFlag.clear(std::memory_order_release); } 0055 bool try_lock() { return !fAFlag.test_and_set(std::memory_order_acquire); } 0056 0057 }; 0058 } 0059 0060 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |