2
0
mirror of https://github.com/boostorg/thread.git synced 2026-02-02 09:22:10 +00:00
Files
thread/doc/tss.html
2001-07-18 14:15:44 +00:00

156 lines
3.5 KiB
HTML

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="keywords" content="threads, BTL, thread library, C++">
<link rel="stylesheet" type="text/css" href="styles.css">
<title>Boost.Threads, tss</title>
</head>
<body bgcolor="#FFFFFF" link="#0000FF" vlink="#800080">
<table border="0" cellpadding="7" cellspacing="0" width="100%">
<tr>
<td valign="top" width="300">
<h3><img src="../../../c++boost.gif" alt="C++ Boost" width="277" height="86"></h3>
</td>
<td valign="top">
<h1 align="center">Boost.Threads</h1>
<h2 align="center">tss</h2>
</td>
</tr>
</table>
<hr>
<p><A href="#Introduction">Introduction</A><br>
<A href="#Header">Header</A><br>
<A href="#Synopsis">Synopsis</A><br>
<A href="#Members">Members</A><br>
<A href="#Example">Example</A></p>
<h2><a name="Introduction">Introduction</a></h2>
<p>The <code>tss</code> class defines an interface for using thread specific storage. Thread
specific storage is data associated with individual threads and is often used to make
operations that rely on global data thread safe.</p>
<h2><a name="Header">Header</a></h2>
<pre>
#include <a href="../../../boost/thread/tss.hpp">&lt;boost/thread/tss.hpp&gt;</a>
</pre>
<h2><a name="Synopsis">Synopsis</a></h2>
<pre>
namespace boost {
class tss : boost::noncopyable
{
public:
tss();
~tss();
void* get() const;
bool set(void* value);
};
} // namespace boost
</pre>
<h2><a name="Members">Members</a></h2>
<hr>
<h3>Constructor</h3>
<pre>
tss();
</pre>
<p><b>Effects:</b> Constructs a <code>tss</code> object for accessing thread specific storage.</p>
<hr>
<h3>Destructor</h3>
<pre>
~tss();
</pre>
<p><b>Effects:</b> Destroys the <code>tss</code> object.</p>
<p><b>Notes:</b> This does not destroy any data that may be stored in the thread specific storage.</p>
<hr>
<h3>get</h3>
<pre>
void* get() const;
</pre>
<p><b>Effects:</b> Retrieves the thread specific storage for the current thread.</p>
<p><b>Notes:</b> If the current thread has not previously called <code>set()</code> the value
returned is <code>0</code>.</p>
<hr>
<h3>set</h3>
<pre>
bool set(void* value);
</pre>
<p><b>Effects:</b> Sets the thread specific storage for the current thread. Returns
<code>false</code> on failure.</p>
<p><b>Notes:</b> Any previous value is simply overwritten and any data that may have been
pointed to by the previous value is not destroyed.</p>
<hr>
<h2><a name="Example">Example Usage</a></h2>
<pre>
#include <a href="../../../boost/thread/thread.hpp">&lt;boost/thread/thread.hpp&gt;</a>
#include <a href="../../../boost/thread/tss.hpp">&lt;boost/thread/tss.hpp&gt;</a>
#include &lt;cassert&gt;
boost::tss value;
void increment()
{
int* p = static_cast<int*>(value.get());
++*p;
}
void thread_proc()
{
value.set(new int(0)); // initialize the thread's storage
for (int i=0; i&lt;10; ++i)
{
increment();
int* p = static_cast<int*>(value.get());
assert(*p == i+1);
}
delete value.get();
}
int main(int argc, char* argv[])
{
boost::thread::thread_group threads;
for (int i=0; i&lt;5; ++i)
threads.create(&amp;thread_proc);
threads.join_all();
}
</pre>
<hr>
<p>Revised <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->17 July, 2001<!--webbot bot="Timestamp" endspan i-checksum="21075" -->
</p>
<p><i>Copyright <a href="mailto:williamkempf@hotmail.com">William E. Kempf</a>
2001 all rights reserved.</i></p>
</body>
</html>