Debian Codegraph @

docs/programmer_reference/stl_mt_usage.html

raw symbols SCIP
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6 <title>Using dbstl in multithreaded applications</title>
7 <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
8 <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
9 <link rel="start" href="index.html" title="Berkeley DB Programmer's Reference Guide" />
10 <link rel="up" href="stl.html" title="Chapter 7. Standard Template Library API" />
11 <link rel="prev" href="stl_txn_usage.html" title="Using transactions in dbstl" />
12 <link rel="next" href="stl_primitive_rw.html" title="Working with primitive types" />
13 </head>
14 <body>
15 <div xmlns="" class="navheader">
16 <div class="libver">
17 <p>Library Version 11.2.5.3</p>
18 </div>
19 <table width="100%" summary="Navigation header">
20 <tr>
21 <th colspan="3" align="center">Using dbstl in multithreaded applications</th>
22 </tr>
23 <tr>
24 <td width="20%" align="left"><a accesskey="p" href="stl_txn_usage.html">Prev</a> </td>
25 <th width="60%" align="center">Chapter 7. Standard Template Library API</th>
26 <td width="20%" align="right"> <a accesskey="n" href="stl_primitive_rw.html">Next</a></td>
27 </tr>
28 </table>
29 <hr />
30 </div>
31 <div class="sect1" lang="en" xml:lang="en">
32 <div class="titlepage">
33 <div>
34 <div>
35 <h2 class="title" style="clear: both"><a id="stl_mt_usage"></a>Using dbstl in multithreaded applications</h2>
36 </div>
37 </div>
38 </div>
39 <p>
40 Multithreaded use of dbstl must obey the following guidelines:
41 </p>
42 <div class="orderedlist">
43 <ol type="1">
44 <li>
45 <p>
46 For a few non-standard platforms, you must first configure dbstl for that
47 platform, but usually the configure script will
48 detect the applicable thread local storage (TLS) modifier to use, and
49 then use it. If no appropriate TLS is found, the pthread TLS API is used.
50 </p>
51 </li>
52 <li>
53 <p>
54 Perform all initializations in a single thread.
55 <code class="methodname">dbstl::dbstl_startup()</code> should be called
56 mutually exclusive in a single thread before using dbstl. If dbstl is
57 used in only a single thread, this function does not need to be called.
58 </p>
59 <p>
60 If necessary, callback functions for a complex type T must be
61 registered to the singleton of
62 DbstlElemTraits&lt;T&gt; before any container related to T (for
63 example, <code class="literal">db_vector&lt;T&gt;</code>), is used, and certain
64 isolation may be required among multiple threads. The best way to do
65 this is to register all callback function pointers into the singleton
66 in a single thread before making use of the containers.
67 </p>
68 <p>
69 All container cursor open flags and auto commit transaction
70 begin/commit flags must be set in a single thread before storing
71 objects into or reading objects from the container.
72 </p>
73 </li>
74 <li>
75 <p>
76 Environment and database handles can optionally be shared across
77 threads. If handles are shared, they must be registered in each thread
78 that is using the handle (either directly, or indirectly using the
79 containers that own the handles). You do this using the
80 <code class="function">dbstl::register_db()</code> and
81 <code class="function">dbstl::register_db_env()</code> functions. Note that
82 these functions are not necessary if the current thread called
83 <code class="function">dbstl::open_db()</code> or
84 <code class="function">dbstl::open_env()</code> for the handle that is being
85 shared. This is because the open functions automatically register the
86 handle for you.
87 </p>
88 <p>
89 Note that the get/set functions that provide access to container data
90 members are not mutex-protected because these data members are supposed
91 to be set only once at container object initialization. Applications
92 wishing to modify them after initialization must supply their own
93 protection.
94 </p>
95 </li>
96 <li>
97 <p>
98 While container objects can be shared between multiple threads,
99 iterators and transactions can not be shared.
100 </p>
101 </li>
102 <li>
103 <p>
104 Set the <span class="bold"><strong>directdb_get</strong></span> parameter of the
105 container <code class="methodname">begin()</code> method to
106 <code class="literal">true</code> in order to guarantee that referenced key/data
107 pairs are always obtained from the database and not from an iterator's
108 cached value. (This is the default behavior.) You should do this
109 because otherwise a rare situation may occur. Given db_vector_iterator
110 i1 and i2 used in the same iteration, setting *i1 = new_value will not
111 update i2, and *i2 will return the original value.
112 </p>
113 </li>
114 <li>
115 <p>
116 If using a CDS database, only const iterators or read-only non-const
117 iterators should be used for read only iterations. Otherwise, when
118 multiple threads try to open read-write iterators at the same time,
119 performance is greatly degraded because CDS only supports one write cursor
120 open at any moment. The use of read-only iterators is good practice
121 in general because dbstl contains internal optimizations for read-only
122 iterators.
123 </p>
124 <p>
125 To create a read-only iterator, do one of the following:
126 </p>
127 <div class="itemizedlist">
128 <ul type="disc">
129 <li>
130 <p>
131 Use a <code class="literal">const</code> reference to the container
132 object, then call the container's
133 <code class="methodname">begin()</code> method using the const
134 reference, and then store the return value from the
135 <code class="methodname">begin()</code> method in a
136 <code class="methodname">db_vector::const_iterator</code>.
137 </p>
138 </li>
139 <li>
140 <p>
141 If you are using a non-const container object, then simply
142 pass <code class="literal">true</code> to the
143 <span class="bold"><strong>readonly</strong></span> parameter of the
144 non-const <code class="methodname">begin()</code> method.
145 </p>
146 </li>
147 </ul>
148 </div>
149 </li>
150 <li>
151 <p>
152 When using DS, CDS or TDS, enable the locking subsystem by passing the
153 <a href="../api_reference/C/envopen.html#envopen_DB_INIT_LOCK" class="olink">DB_INIT_LOCK</a> flag to <code class="methodname">DbEnv::open()</code>.
154 </p>
155 </li>
156 <li>
157 <p>
158 Perform portable thread synchronization within a process by calling the
159 following functions. These are all global functions in the "dbstl" name
160 space:
161 </p>
162 <table class="simplelist" border="0" summary="Simple list">
163 <tr>
164 <td>
165 <code class="function">db_mutex_t alloc_mutex();</code>
166 </td>
167 </tr>
168 <tr>
169 <td>
170 <code class="function">int lock_mutex(db_mutex_t);</code>
171 </td>
172 </tr>
173 <tr>
174 <td>
175 <code class="function">int unlock_mutex(db_mutex_t);</code>
176 </td>
177 </tr>
178 <tr>
179 <td>
180 <code class="function">void free_mutex(db_mutex_t);</code>
181 </td>
182 </tr>
183 </table>
184 <p>
185 These functions use an internal dbstl environment's mutex functionality
186 to synchronize. As a result, the synchronization is portable across all
187 platforms supported by Berkeley DB.
188 </p>
189 </li>
190 </ol>
191 </div>
192 <p>
193 The <code class="classname">WorkerThread</code> class provides example code
194 demonstrating the use of dbstl in multi-threaded applications. You can
195 find this class implemented in the dbstl test suite.
196 </p>
197 </div>
198 <div class="navfooter">
199 <hr />
200 <table width="100%" summary="Navigation footer">
201 <tr>
202 <td width="40%" align="left"><a accesskey="p" href="stl_txn_usage.html">Prev</a> </td>
203 <td width="20%" align="center">
204 <a accesskey="u" href="stl.html">Up</a>
205 </td>
206 <td width="40%" align="right"> <a accesskey="n" href="stl_primitive_rw.html">Next</a></td>
207 </tr>
208 <tr>
209 <td width="40%" align="left" valign="top">Using transactions in dbstl </td>
210 <td width="20%" align="center">
211 <a accesskey="h" href="index.html">Home</a>
212 </td>
213 <td width="40%" align="right" valign="top"> Working with primitive types </td>
214 </tr>
215 </table>
216 </div>
217 </body>
218 </html>