2
0
mirror of https://github.com/boostorg/pool.git synced 2026-01-24 18:12:34 +00:00
Files
pool/doc/implementation/guard.html
Beman Dawes 0a122496e1 Initial commit
[SVN r9503]
2001-03-08 20:44:22 +00:00

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&lt;Mutex&gt;</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 &lt;typename Mutex&gt;
class guard
{
private:
guard(const guard &amp;);
void operator=(const guard &amp;);
public:
explicit guard(Mutex &amp; 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&lt;Mutex&gt;</SPAN>
<TR><TD CLASS="code">m<TD>value of type <SPAN CLASS="code">Mutex &amp;</SPAN>
<TR><TD CLASS="code">g<TD>value of type <SPAN CLASS="code">guard&lt;Mutex&gt;</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">(&amp;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&lt;mutex&gt; g(global_lock);
// g's constructor locks &quot;global_lock&quot;
... // do anything:
// throw exceptions
// return
// or just fall through
} // g's destructor unlocks &quot;global_lock&quot;</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 &copy; 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 &quot;as is&quot; without express or implied warranty, and with no claim as to its suitability for any purpose.
</BODY>
</HTML>