Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 10:05:01

0001 // Copyright (C) 2000 Stephen Cleary
0002 //
0003 // Distributed under the Boost Software License, Version 1.0. (See
0004 // accompanying file LICENSE_1_0.txt or copy at
0005 // http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // See http://www.boost.org for updates, documentation, and revision history.
0008 
0009 #ifndef BOOST_POOL_GUARD_HPP
0010 #define BOOST_POOL_GUARD_HPP
0011 
0012 /*!
0013   \file
0014   \brief Extremely Light-Weight guard class.
0015   \details Auto-lock/unlock-er
0016   detail/guard.hpp provides a type guard<Mutex>
0017   that allows scoped access to the Mutex's locking and unlocking operations.
0018   It is used to ensure that a Mutex is unlocked, even if an exception is thrown.
0019 */
0020 
0021 namespace boost {
0022 
0023 namespace details {
0024 namespace pool {
0025 
0026 template <typename Mutex> //!< \tparam Mutex (platform-specific) mutex class.
0027 class guard
0028 { //! Locks the mutex, binding guard<Mutex> to Mutex.
0029     /*! Example:
0030     Given a (platform-specific) mutex class, we can wrap code as follows:
0031 
0032     extern mutex global_lock;
0033 
0034     static void f()
0035     {
0036         boost::details::pool::guard<mutex> g(global_lock);
0037         // g's constructor locks "global_lock"
0038 
0039         ... // do anything:
0040                 //   throw exceptions
0041                 //   return
0042                 //   or just fall through
0043     } // g's destructor unlocks "global_lock"
0044     */
0045   private:
0046     Mutex & mtx;
0047 
0048     guard(const guard &); //!< Guards the mutex, ensuring unlocked on destruction, even if exception is thrown.
0049     void operator=(const guard &);
0050 
0051   public:
0052     explicit guard(Mutex & nmtx)
0053     :mtx(nmtx)
0054     { //! Locks the mutex of the guard class.
0055             mtx.lock();
0056         }
0057 
0058     ~guard()
0059     { //! destructor unlocks the mutex of the guard class.
0060             mtx.unlock();
0061         }
0062 }; // class guard
0063 
0064 } // namespace pool
0065 } // namespace details
0066 
0067 } // namespace boost
0068 
0069 #endif