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>Chapter 4. Concurrency</title>
7    <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
8    <meta name="generator" content="DocBook XSL Stylesheets V1.62.4" />
9    <link rel="home" href="index.html" title="Getting Started with Berkeley DB Transaction Processing" />
10    <link rel="up" href="index.html" title="Getting Started with Berkeley DB Transaction Processing" />
11    <link rel="previous" href="maxtxns.html" title="Configuring the Transaction Subsystem" />
12    <link rel="next" href="blocking_deadlocks.html" title="Locks, Blocks, and Deadlocks" />
13  </head>
14  <body>
15    <div class="navheader">
16      <table width="100%" summary="Navigation header">
17        <tr>
18          <th colspan="3" align="center">Chapter 4. Concurrency</th>
19        </tr>
20        <tr>
21          <td width="20%" align="left"><a accesskey="p" href="maxtxns.html">Prev</a> </td>
22          <th width="60%" align="center"> </th>
23          <td width="20%" align="right"> <a accesskey="n" href="blocking_deadlocks.html">Next</a></td>
24        </tr>
25      </table>
26      <hr />
27    </div>
28    <div class="chapter" lang="en" xml:lang="en">
29      <div class="titlepage">
30        <div>
31          <div>
32            <h2 class="title"><a id="txnconcurrency"></a>Chapter 4. Concurrency</h2>
33          </div>
34        </div>
35        <div></div>
36      </div>
37      <div class="toc">
38        <p>
39          <b>Table of Contents</b>
40        </p>
41        <dl>
42          <dt>
43            <span class="sect1">
44              <a href="txnconcurrency.html#concurrenthandles">Which DB Handles are Free-Threaded</a>
45            </span>
46          </dt>
47          <dt>
48            <span class="sect1">
49              <a href="blocking_deadlocks.html">Locks, Blocks, and Deadlocks</a>
50            </span>
51          </dt>
52          <dd>
53            <dl>
54              <dt>
55                <span class="sect2">
56                  <a href="blocking_deadlocks.html#locks">Locks</a>
57                </span>
58              </dt>
59              <dt>
60                <span class="sect2">
61                  <a href="blocking_deadlocks.html#blocks">Blocks</a>
62                </span>
63              </dt>
64              <dt>
65                <span class="sect2">
66                  <a href="blocking_deadlocks.html#deadlocks">Deadlocks</a>
67                </span>
68              </dt>
69            </dl>
70          </dd>
71          <dt>
72            <span class="sect1">
73              <a href="lockingsubsystem.html">The Locking Subsystem</a>
74            </span>
75          </dt>
76          <dd>
77            <dl>
78              <dt>
79                <span class="sect2">
80                  <a href="lockingsubsystem.html#configuringlock">Configuring the Locking Subsystem</a>
81                </span>
82              </dt>
83              <dt>
84                <span class="sect2">
85                  <a href="lockingsubsystem.html#configdeadlkdetect">Configuring Deadlock Detection</a>
86                </span>
87              </dt>
88              <dt>
89                <span class="sect2">
90                  <a href="lockingsubsystem.html#deadlockresolve">Resolving Deadlocks</a>
91                </span>
92              </dt>
93            </dl>
94          </dd>
95          <dt>
96            <span class="sect1">
97              <a href="isolation.html">Isolation</a>
98            </span>
99          </dt>
100          <dd>
101            <dl>
102              <dt>
103                <span class="sect2">
104                  <a href="isolation.html#degreesofisolation">Supported Degrees of Isolation</a>
105                </span>
106              </dt>
107              <dt>
108                <span class="sect2">
109                  <a href="isolation.html#dirtyreads">Reading Uncommitted Data</a>
110                </span>
111              </dt>
112              <dt>
113                <span class="sect2">
114                  <a href="isolation.html#readcommitted">Committed Reads</a>
115                </span>
116              </dt>
117              <dt>
118                <span class="sect2">
119                  <a href="isolation.html#snapshot_isolation">Using Snapshot Isolation</a>
120                </span>
121              </dt>
122            </dl>
123          </dd>
124          <dt>
125            <span class="sect1">
126              <a href="txn_ccursor.html">Transactional Cursors and Concurrent Applications</a>
127            </span>
128          </dt>
129          <dd>
130            <dl>
131              <dt>
132                <span class="sect2">
133                  <a href="txn_ccursor.html#cursordirtyreads">Using Cursors with Uncommitted Data</a>
134                </span>
135              </dt>
136            </dl>
137          </dd>
138          <dt>
139            <span class="sect1">
140              <a href="readmodifywrite.html">Read/Modify/Write</a>
141            </span>
142          </dt>
143          <dt>
144            <span class="sect1">
145              <a href="txnnowait.html">No Wait on Blocks</a>
146            </span>
147          </dt>
148          <dt>
149            <span class="sect1">
150              <a href="reversesplit.html">Reverse BTree Splits</a>
151            </span>
152          </dt>
153        </dl>
154      </div>
155      <p>
156
157        DB offers a great deal of support for multi-threaded
158            <span>
159                and multi-process
160            </span>
161        applications even when transactions are not in use. Many of DB's
162        handles are 
163        thread-safe<span>, or 
164        can be made thread-safe by providing the appropriate flag at handle creation time,</span> 
165        and DB provides a
166        flexible locking subsystem for managing databases in a concurrent
167        application. Further, DB provides a robust mechanism for
168        detecting and responding to deadlocks. All of these concepts are
169        explored in this chapter.
170     </p>
171      <p>
172        Before continuing, it is useful to define a few terms that will appear
173        throughout this chapter:
174     </p>
175      <div class="itemizedlist">
176        <ul type="disc">
177          <li>
178            <p>
179                <span class="emphasis"><em>Thread of control</em></span>
180            </p>
181            <p>
182                Refers to a thread that is performing work in your application.
183                Typically, in this book that thread will be performing DB
184                operations.
185            </p>
186            <p>
187                Note that this term can also be taken to mean a separate process
188                that is performing work — DB supports multi-process
189                operations on your databases.
190            </p>
191            <p>
192                Also, DB is agnostic with regard to the type or style of
193                threads in use in your application. So if you are using
194                multiple threads
195                (as opposed to multiple processes) to perform concurrent
196                database access, you are free to use whatever thread package is
197                best for your platform and application. That said, this manual
198                will use pthreads for its threading examples because those have
199                the best chance of being supported across a large range of
200                platforms.
201            </p>
202          </li>
203          <li>
204            <p>
205                <span class="emphasis"><em>Locking</em></span>
206            </p>
207            <p>
208                When a thread of control obtains 
209                access to a shared resource, it is said to be
210                <span class="emphasis"><em>locking</em></span> that resource. Note that
211                DB supports both exclusive and non-exclusive locks. See
212                <a href="blocking_deadlocks.html#locks">Locks</a> for more information.
213            </p>
214          </li>
215          <li>
216            <p>
217                <span class="emphasis"><em>Free-threaded</em></span>
218            </p>
219            <p>
220                Data structures and objects are free-threaded if they can be
221                shared across threads of control without any explicit locking on
222                the part of the application.  Some books, libraries, and
223                programming languages may use the term
224                <span class="emphasis"><em>thread-safe</em></span> for data structures or objects
225                that have this characteristic. The two terms mean the
226                same thing.
227            </p>
228            <p>
229                For a description of free-threaded DB objects, see
230                <a href="txnconcurrency.html#concurrenthandles">Which DB Handles are Free-Threaded</a>.
231            </p>
232          </li>
233          <li>
234            <p>
235                <span class="emphasis"><em>Blocked</em></span>
236            </p>
237            <p>
238                When a thread cannot obtain a lock because some other
239                thread already holds a lock on that object, the lock
240                attempt is said to be <span class="emphasis"><em>blocked</em></span>. See
241                <a href="blocking_deadlocks.html#blocks">Blocks</a> for more information.
242            </p>
243          </li>
244          <li>
245            <p>
246                <span class="emphasis"><em>Deadlock</em></span>
247            </p>
248            <p>
249                Occurs when two or more threads of control attempt to access conflicting resource in such a way as none
250                of the threads can any longer may further progress.
251            </p>
252            <p>
253                For example, if Thread A is blocked waiting for a resource held by Thread
254                B, while at the same time Thread B is blocked waiting for a
255                resource held by Thread A, then neither thread can make any
256                forward progress. In this situation, Thread A and Thread B
257                are said to be <span class="emphasis"><em>deadlocked.</em></span>
258            </p>
259            <p>
260                For more information, see <a href="blocking_deadlocks.html#deadlocks">Deadlocks</a>.
261            </p>
262          </li>
263        </ul>
264      </div>
265      <div class="sect1" lang="en" xml:lang="en">
266        <div class="titlepage">
267          <div>
268            <div>
269              <h2 class="title" style="clear: both"><a id="concurrenthandles"></a>Which DB Handles are Free-Threaded</h2>
270            </div>
271          </div>
272          <div></div>
273        </div>
274        <p>
275            The following describes to what extent and under what conditions
276            individual handles are free-threaded.
277        </p>
278        <div class="itemizedlist">
279          <ul type="disc">
280            <li>
281              <p>
282                    
283                   <tt class="classname">DbEnv</tt> 
284                    
285                </p>
286              <p>
287                    Free-threaded so long as
288                        <span>
289                            the <tt class="literal">DB_THREAD</tt> flag is provided to
290                            the environment <tt class="methodname">open()</tt>
291                            method.
292                        </span>
293                        
294                </p>
295            </li>
296            <li>
297              <p>
298                    
299                   <tt class="classname">Db</tt> 
300                    
301                </p>
302              <p>
303                    Free-threaded so long as
304                        <span>
305                            the <tt class="literal">DB_THREAD</tt> flag is provided to
306                            the database <tt class="methodname">open()</tt>
307                            method, or if the database is opened using a
308                            free-threaded environment handle.
309                        </span>
310                        
311                </p>
312            </li>
313            <li>
314              <p>
315                    
316                   <tt class="classname">Dbc</tt> 
317                    
318                </p>
319              <p>
320                    Cursors are not free-threaded. However, they
321                    can be used by multiple threads of control so 
322                    long as the application serializes access to the handle.
323                </p>
324            </li>
325            <li>
326              <p>
327                    
328                   <tt class="classname">DbTxn</tt> 
329                    
330                    
331                </p>
332              <p>
333                    Access must be serialized by the application across threads of control.
334                </p>
335            </li>
336          </ul>
337        </div>
338      </div>
339    </div>
340    <div class="navfooter">
341      <hr />
342      <table width="100%" summary="Navigation footer">
343        <tr>
344          <td width="40%" align="left"><a accesskey="p" href="maxtxns.html">Prev</a> </td>
345          <td width="20%" align="center">
346            <a accesskey="u" href="index.html">Up</a>
347          </td>
348          <td width="40%" align="right"> <a accesskey="n" href="blocking_deadlocks.html">Next</a></td>
349        </tr>
350        <tr>
351          <td width="40%" align="left" valign="top">Configuring the Transaction Subsystem </td>
352          <td width="20%" align="center">
353            <a accesskey="h" href="index.html">Home</a>
354          </td>
355          <td width="40%" align="right" valign="top"> Locks, Blocks, and Deadlocks</td>
356        </tr>
357      </table>
358    </div>
359  </body>
360</html>
361