1 | <?xml version="1.0" encoding="utf-8"?> |
---|
2 | <!DOCTYPE library PUBLIC "-//Boost//DTD BoostBook XML V1.0//EN" |
---|
3 | "http://www.boost.org/tools/boostbook/dtd/boostbook.dtd" [ |
---|
4 | <!ENTITY % threads.entities SYSTEM "entities.xml"> |
---|
5 | %threads.entities; |
---|
6 | ]> |
---|
7 | <header name="boost/thread/tss.hpp" |
---|
8 | last-revision="$Date: 2004/07/17 04:33:59 $"> |
---|
9 | <namespace name="boost"> |
---|
10 | <class name="thread_specific_ptr"> |
---|
11 | <purpose> |
---|
12 | The <classname>thread_specific_ptr</classname> class defines |
---|
13 | an interface for using thread specific storage. |
---|
14 | </purpose> |
---|
15 | |
---|
16 | <description> |
---|
17 | <para>Thread specific storage is data associated with |
---|
18 | individual threads and is often used to make operations |
---|
19 | that rely on global data |
---|
20 | <link linkend="threads.glossary.thread-safe">thread-safe</link>. |
---|
21 | </para> |
---|
22 | |
---|
23 | <para>Template <classname>thread_specific_ptr</classname> |
---|
24 | stores a pointer to an object obtained on a thread-by-thread |
---|
25 | basis and calls a specified cleanup handler on the contained |
---|
26 | pointer when the thread terminates. The cleanup handlers are |
---|
27 | called in the reverse order of construction of the |
---|
28 | <classname>thread_specific_ptr</classname>s, and for the |
---|
29 | initial thread are called by the destructor, providing the |
---|
30 | same ordering guarantees as for normal declarations. Each |
---|
31 | thread initially stores the null pointer in each |
---|
32 | <classname>thread_specific_ptr</classname> instance.</para> |
---|
33 | |
---|
34 | <para>The template <classname>thread_specific_ptr</classname> |
---|
35 | is useful in the following cases: |
---|
36 | <itemizedlist> |
---|
37 | <listitem>An interface was originally written assuming |
---|
38 | a single thread of control and it is being ported to a |
---|
39 | multithreaded environment.</listitem> |
---|
40 | |
---|
41 | <listitem>Each thread of control invokes sequences of |
---|
42 | methods that share data that are physically unique |
---|
43 | for each thread, but must be logically accessed |
---|
44 | through a globally visible access point instead of |
---|
45 | being explicitly passed.</listitem> |
---|
46 | </itemizedlist> |
---|
47 | </para> |
---|
48 | </description> |
---|
49 | |
---|
50 | <inherit access="private"> |
---|
51 | <type><classname>boost::noncopyable</classname></type> |
---|
52 | <purpose>Exposition only</purpose> |
---|
53 | </inherit> |
---|
54 | |
---|
55 | <constructor> |
---|
56 | <requires>The expression <code>delete get()</code> is well |
---|
57 | formed.</requires> |
---|
58 | |
---|
59 | <effects>A thread-specific data key is allocated and visible to |
---|
60 | all threads in the process. Upon creation, the value |
---|
61 | <code>NULL</code> will be associated with the new key in all |
---|
62 | active threads. A cleanup method is registered with the key |
---|
63 | that will call <code>delete</code> on the value associated |
---|
64 | with the key for a thread when it exits. When a thread exits, |
---|
65 | if a key has a registered cleanup method and the thread has a |
---|
66 | non-<code>NULL</code> value associated with that key, the value |
---|
67 | of the key is set to <code>NULL</code> and then the cleanup |
---|
68 | method is called with the previously associated value as its |
---|
69 | sole argument. The order in which registered cleanup methods |
---|
70 | are called when a thread exits is undefined. If after all the |
---|
71 | cleanup methods have been called for all non-<code>NULL</code> |
---|
72 | values, there are still some non-<code>NULL</code> values |
---|
73 | with associated cleanup handlers the result is undefined |
---|
74 | behavior.</effects> |
---|
75 | |
---|
76 | <throws><classname>boost::thread_resource_error</classname> if |
---|
77 | the necessary resources can not be obtained.</throws> |
---|
78 | |
---|
79 | <notes>There may be an implementation specific limit to the |
---|
80 | number of thread specific storage objects that can be created, |
---|
81 | and this limit may be small.</notes> |
---|
82 | |
---|
83 | <rationale>The most common need for cleanup will be to call |
---|
84 | <code>delete</code> on the associated value. If other forms |
---|
85 | of cleanup are required the overloaded constructor should be |
---|
86 | called instead.</rationale> |
---|
87 | </constructor> |
---|
88 | |
---|
89 | <constructor> |
---|
90 | <parameter name="cleanup"> |
---|
91 | <paramtype>void (*cleanup)(void*)</paramtype> |
---|
92 | </parameter> |
---|
93 | |
---|
94 | <effects>A thread-specific data key is allocated and visible to |
---|
95 | all threads in the process. Upon creation, the value |
---|
96 | <code>NULL</code> will be associated with the new key in all |
---|
97 | active threads. The <code>cleanup</code> method is registered |
---|
98 | with the key and will be called for a thread with the value |
---|
99 | associated with the key for that thread when it exits. When a |
---|
100 | thread exits, if a key has a registered cleanup method and the |
---|
101 | thread has a non-<code>NULL</code> value associated with that |
---|
102 | key, the value of the key is set to <code>NULL</code> and then |
---|
103 | the cleanup method is called with the previously associated |
---|
104 | value as its sole argument. The order in which registered |
---|
105 | cleanup methods are called when a thread exits is undefined. |
---|
106 | If after all the cleanup methods have been called for all |
---|
107 | non-<code>NULL</code> values, there are still some |
---|
108 | non-<code>NULL</code> values with associated cleanup handlers |
---|
109 | the result is undefined behavior.</effects> |
---|
110 | |
---|
111 | <throws><classname>boost::thread_resource_error</classname> if |
---|
112 | the necessary resources can not be obtained.</throws> |
---|
113 | |
---|
114 | <notes>There may be an implementation specific limit to the |
---|
115 | number of thread specific storage objects that can be created, |
---|
116 | and this limit may be small.</notes> |
---|
117 | |
---|
118 | <rationale>There is the occasional need to register |
---|
119 | specialized cleanup methods, or to register no cleanup method |
---|
120 | at all (done by passing <code>NULL</code> to this constructor. |
---|
121 | </rationale> |
---|
122 | </constructor> |
---|
123 | |
---|
124 | <destructor> |
---|
125 | <effects>Deletes the thread-specific data key allocated by the |
---|
126 | constructor. The thread-specific data values associated with |
---|
127 | the key need not be <code>NULL</code>. It is the responsibility |
---|
128 | of the application to perform any cleanup actions for data |
---|
129 | associated with the key.</effects> |
---|
130 | |
---|
131 | <notes>Does not destroy any data that may be stored in any |
---|
132 | thread's thread specific storage. For this reason you should |
---|
133 | not destroy a <classname>thread_specific_ptr</classname> object |
---|
134 | until you are certain there are no threads running that have |
---|
135 | made use of its thread specific storage.</notes> |
---|
136 | |
---|
137 | <rationale>Associated data is not cleaned up because registered |
---|
138 | cleanup methods need to be run in the thread that allocated the |
---|
139 | associated data to be guarranteed to work correctly. There's no |
---|
140 | safe way to inject the call into another thread's execution |
---|
141 | path, making it impossible to call the cleanup methods safely. |
---|
142 | </rationale> |
---|
143 | </destructor> |
---|
144 | |
---|
145 | <method-group name="modifier functions"> |
---|
146 | <method name="release"> |
---|
147 | <type>T*</type> |
---|
148 | |
---|
149 | <postconditions><code>*this</code> holds the null pointer |
---|
150 | for the current thread.</postconditions> |
---|
151 | |
---|
152 | <returns><code>this->get()</code> prior to the call.</returns> |
---|
153 | |
---|
154 | <rationale>This method provides a mechanism for the user to |
---|
155 | relinquish control of the data associated with the |
---|
156 | thread-specific key.</rationale> |
---|
157 | </method> |
---|
158 | |
---|
159 | <method name="reset"> |
---|
160 | <type>void</type> |
---|
161 | |
---|
162 | <parameter name="p"> |
---|
163 | <paramtype>T*</paramtype> |
---|
164 | <default>0</default> |
---|
165 | </parameter> |
---|
166 | |
---|
167 | <effects>If <code>this->get() != p && |
---|
168 | this->get() != NULL</code> then call the |
---|
169 | associated cleanup function.</effects> |
---|
170 | |
---|
171 | <postconditions><code>*this</code> holds the pointer |
---|
172 | <code>p</code> for the current thread.</postconditions> |
---|
173 | </method> |
---|
174 | </method-group> |
---|
175 | |
---|
176 | <method-group name="observer functions"> |
---|
177 | <method name="get" cv="const"> |
---|
178 | <type>T*</type> |
---|
179 | |
---|
180 | <returns>The object stored in thread specific storage for |
---|
181 | the current thread for <code>*this</code>.</returns> |
---|
182 | |
---|
183 | <notes>Each thread initially returns 0.</notes> |
---|
184 | </method> |
---|
185 | |
---|
186 | <method name="operator->" cv="const"> |
---|
187 | <type>T*</type> |
---|
188 | |
---|
189 | <returns><code>this->get()</code>.</returns> |
---|
190 | </method> |
---|
191 | |
---|
192 | <method name="operator*()" cv="const"> |
---|
193 | <type>T&</type> |
---|
194 | |
---|
195 | <requires><code>this->get() != 0</code></requires> |
---|
196 | |
---|
197 | <returns><code>this->get()</code>.</returns> |
---|
198 | </method> |
---|
199 | </method-group> |
---|
200 | </class> |
---|
201 | </namespace> |
---|
202 | </header> |
---|