guard - Auto-lock/unlock-erIntroductiondetail/guard.hpp provides a type guard<Mutex> that allows scoped access to the Mutex's locking and unlocking operations. It is used to ensure that a Mutex is unlocked, even if an exception is thrown. Synopsisnamespace details { namespace pool { template <typename Mutex> class guard { private: guard(const guard &); void operator=(const guard &); public: explicit guard(Mutex & mtx); ~guard(); }; } // namespace pool } // namespace details Semantics
ExampleGiven a (platform-specific) mutex class, we can wrap code as follows: extern mutex global_lock; static void f() { boost::details::pool::guard<mutex> g(global_lock); // g's constructor locks "global_lock" ... // do anything: // throw exceptions // return // or just fall through } // g's destructor unlocks "global_lock" DependenciesNone. Future DirectionsThis header will eventually be replaced by a Boost multithreading library. Revised 05 December, 2006 Copyright © 2000, 2001 Stephen Cleary (scleary AT jerviswebb DOT com) Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |