mirror of
https://github.com/boostorg/pool.git
synced 2026-01-24 06:02:13 +00:00
111 lines
3.2 KiB
HTML
111 lines
3.2 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
|
<HTML>
|
|
<HEAD>
|
|
<TITLE>guard - Auto-lock/unlock-er</TITLE>
|
|
<LINK HREF="../pool.css" REL="stylesheet" TYPE="text/css">
|
|
</HEAD>
|
|
<BODY>
|
|
|
|
<IMG SRC="../../../../c++boost.gif" WIDTH=276 HEIGHT=86 ALT="C++ Boost">
|
|
|
|
<H1 ALIGN=CENTER>guard - Auto-lock/unlock-er</H1>
|
|
|
|
<P>
|
|
<H2>Introduction</H2>
|
|
|
|
<P>
|
|
detail/guard.hpp provides a type <SPAN CLASS="code">guard<Mutex></SPAN> that allows scoped access to the <SPAN CLASS="code">Mutex</SPAN>'s locking and unlocking operations. It is used to ensure that a <SPAN CLASS="code">Mutex</SPAN> is unlocked, even if an exception is thrown.
|
|
|
|
<P>
|
|
<H2>Synopsis</H2>
|
|
|
|
<PRE CLASS="code">namespace 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</PRE>
|
|
|
|
<P>
|
|
<H2>Semantics</H2>
|
|
|
|
<P>
|
|
<TABLE BORDER ALIGN=CENTER>
|
|
<CAPTION><EM>Symbol Table</EM></CAPTION>
|
|
<TR><TH>Symbol<TH>Meaning
|
|
<TR><TD CLASS="code">T<TD><SPAN CLASS="code">guard<Mutex></SPAN>
|
|
<TR><TD CLASS="code">m<TD>value of type <SPAN CLASS="code">Mutex &</SPAN>
|
|
<TR><TD CLASS="code">g<TD>value of type <SPAN CLASS="code">guard<Mutex></SPAN>
|
|
</TABLE>
|
|
|
|
<P>
|
|
<TABLE BORDER ALIGN=CENTER>
|
|
<CAPTION><EM>Requirements on <SPAN CLASS="code">Mutex</SPAN></EM></CAPTION>
|
|
<TR><TH>Expression<TH>Return Type<TH>Assertion/Note/Pre/Post-Condition
|
|
<TR><TD CLASS="code">m.lock()<TD>not used<TD>Locks the mutex referred to by <SPAN CLASS="code">m</SPAN>
|
|
<TR><TD CLASS="code">m.unlock()<TD>not used<TD>Unlocks the mutex referred to by <SPAN CLASS="code">m</SPAN>
|
|
</TABLE>
|
|
|
|
<P>
|
|
<TABLE BORDER ALIGN=CENTER>
|
|
<CAPTION><EM>Requirements satisfied by <SPAN CLASS="code">guard</SPAN></EM></CAPTION>
|
|
<TR><TH>Expression<TH>Assertion/Note/Pre/Post-Condition
|
|
<TR><TD CLASS="code">T(m)<TD>Locks the mutex referred to by <SPAN CLASS="code">m</SPAN>; binds <SPAN CLASS="code">T(m)</SPAN> to <SPAN CLASS="code">m</SPAN>
|
|
<TR><TD CLASS="code">(&g)->~T()<TD>Unlocks the mutex that <SPAN CLASS="code">g</SPAN> is bound to
|
|
</TABLE>
|
|
|
|
<P>
|
|
<H2>Example</H2>
|
|
|
|
<P>
|
|
Given a (platform-specific) <SPAN CLASS="code">mutex</SPAN> class, we can wrap code as follows:
|
|
<PRE CLASS="code">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"</PRE>
|
|
|
|
<P>
|
|
<H2>Dependencies</H2>
|
|
|
|
<P>
|
|
None.
|
|
|
|
<P>
|
|
<H2>Future Directions</H2>
|
|
|
|
<P>
|
|
This header will eventually be replaced by a Boost multithreading library.
|
|
|
|
<P>
|
|
<HR>
|
|
|
|
<P>
|
|
Copyright © 2000, 2001 Stephen Cleary (<A HREF="mailto:shammah@voyager.net">shammah@voyager.net</A>)
|
|
|
|
<P>
|
|
This file can be redistributed and/or modified under the terms found in <A HREF="../copyright.html">copyright.html</A>
|
|
|
|
<P>
|
|
This software and its documentation is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.
|
|
|
|
</BODY>
|
|
</HTML> |