1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
<!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>
|