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