| 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>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="program.html" title="Chapter 15. Programmer Notes" /> |
| 11 | <link rel="prev" href="program_environ.html" title="Environment variables" /> |
| 12 | <link rel="next" href="program_scope.html" title="Berkeley DB handles" /> |
| 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">Multithreaded applications</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="program_environ.html">Prev</a> </td> |
| 25 | <th width="60%" align="center">Chapter 15. |
| 26 | Programmer Notes |
| 27 | </th> |
| 28 | <td width="20%" align="right"> <a accesskey="n" href="program_scope.html">Next</a></td> |
| 29 | </tr> |
| 30 | </table> |
| 31 | <hr /> |
| 32 | </div> |
| 33 | <div class="sect1" lang="en" xml:lang="en"> |
| 34 | <div class="titlepage"> |
| 35 | <div> |
| 36 | <div> |
| 37 | <h2 class="title" style="clear: both"><a id="program_mt"></a>Multithreaded applications</h2> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 | <p>Berkeley DB fully supports multithreaded applications. The Berkeley DB library is |
| 42 | not itself multithreaded, and was deliberately architected to not use |
| 43 | threads internally because of the portability problems that would |
| 44 | introduce. Database environment and database object handles returned |
| 45 | from Berkeley DB library functions are free-threaded. No other object handles |
| 46 | returned from the Berkeley DB library are free-threaded. The following rules |
| 47 | should be observed when using threads to access the Berkeley DB library:</p> |
| 48 | <div class="orderedlist"> |
| 49 | <ol type="1"> |
| 50 | <li> |
| 51 | <p>The <a href="../api_reference/C/dbopen.html#open_DB_THREAD" class="olink">DB_THREAD</a> flag must be specified to the <a href="../api_reference/C/envopen.html" class="olink">DB_ENV->open()</a> |
| 52 | and <a href="../api_reference/C/dbopen.html" class="olink">DB->open()</a> methods if the Berkeley DB handles returned by those interfaces |
| 53 | will be used in the context of more than one thread. Setting the |
| 54 | <a href="../api_reference/C/dbopen.html#open_DB_THREAD" class="olink">DB_THREAD</a> flag inconsistently may result in database corruption. |
| 55 | </p> |
| 56 | <p>Threading is assumed in the Java API, so no special flags are required; |
| 57 | and Berkeley DB functions will always behave as if the <a href="../api_reference/C/dbopen.html#open_DB_THREAD" class="olink">DB_THREAD</a> flag |
| 58 | was specified.</p> |
| 59 | <p>Only a single thread may call the <a href="../api_reference/C/envclose.html" class="olink">DB_ENV->close()</a> or <a href="../api_reference/C/dbclose.html" class="olink">DB->close()</a> methods |
| 60 | for a returned environment or database handle.</p> |
| 61 | <p>No other Berkeley DB handles are free-threaded.</p> |
| 62 | </li> |
| 63 | <li> |
| 64 | <p> |
| 65 | When using the non-cursor Berkeley DB calls to retrieve key/data items |
| 66 | (for example, <a href="../api_reference/C/dbget.html" class="olink">DB->get()</a>), the memory to which the pointer stored into the |
| 67 | Dbt refers is valid only until the next call using the <a href="../api_reference/C/db.html" class="olink">DB</a> |
| 68 | handle returned by <a href="../api_reference/C/dbopen.html" class="olink">DB->open()</a>. This includes <span class="bold"><strong>any</strong></span> use of the returned <a href="../api_reference/C/db.html" class="olink">DB</a> handle, |
| 69 | including by another thread within the process. |
| 70 | </p> |
| 71 | <p>For this reason, if the <a href="../api_reference/C/dbopen.html#open_DB_THREAD" class="olink">DB_THREAD</a> handle was specified to the |
| 72 | <a href="../api_reference/C/dbopen.html" class="olink">DB->open()</a> method, either <a href="../api_reference/C/dbt.html#dbt_DB_DBT_MALLOC" class="olink">DB_DBT_MALLOC</a>, <a href="../api_reference/C/dbt.html#dbt_DB_DBT_REALLOC" class="olink">DB_DBT_REALLOC</a> or <a href="../api_reference/C/dbt.html#dbt_DB_DBT_USERMEM" class="olink">DB_DBT_USERMEM</a> |
| 73 | must be specified in the <a href="../api_reference/C/dbt.html" class="olink">DBT</a> when |
| 74 | performing any non-cursor key or data retrieval.</p> |
| 75 | </li> |
| 76 | <li> |
| 77 | <p>Cursors may not span transactions. Each cursor must be |
| 78 | allocated and deallocated within the same transaction.</p> |
| 79 | <p>Transactions and cursors may span threads, but only serially, that is, |
| 80 | the application must serialize access to the <a href="../api_reference/C/txn.html" class="olink">TXN</a> and |
| 81 | <a href="../api_reference/C/dbc.html" class="olink">DBC</a> handles. In the case of nested transactions, since all |
| 82 | child transactions are part of the same parent transaction, they must observe |
| 83 | the same constraints. That is, children may execute in different threads |
| 84 | only if each child executes serially.</p> |
| 85 | </li> |
| 86 | <li> |
| 87 | <p>User-level synchronization mutexes must have been implemented for the |
| 88 | compiler/architecture combination. Attempting to specify the DB_THREAD |
| 89 | flag will fail if fast mutexes are not available. |
| 90 | </p> |
| 91 | <p>If blocking mutexes are available (for example POSIX pthreads), they |
| 92 | will be used. Otherwise, the Berkeley DB library will make a system call to |
| 93 | pause for some amount of time when it is necessary to wait on a lock. |
| 94 | This may not be optimal, especially in a thread-only environment, in |
| 95 | which it is usually more efficient to explicitly yield the processor to |
| 96 | another thread.</p> |
| 97 | <p>It is possible to specify a yield function on an per-application basis. |
| 98 | See <a href="../api_reference/C/db_env_set_func_yield.html" class="olink">db_env_set_func_yield</a> for more information.</p> |
| 99 | <p>It is possible to specify the number of attempts that will be made to |
| 100 | acquire the mutex before waiting. See <a href="../api_reference/C/mutexset_tas_spins.html" class="olink">DB_ENV->mutex_set_tas_spins()</a> for |
| 101 | more information.</p> |
| 102 | </li> |
| 103 | </ol> |
| 104 | </div> |
| 105 | <p>When creating multiple databases in a single physical file, multithreaded |
| 106 | programs may have additional requirements. For more information, see |
| 107 | <a class="xref" href="am_opensub.html" title="Opening multiple databases in a single file">Opening multiple databases in a single file</a></p> |
| 108 | </div> |
| 109 | <div class="navfooter"> |
| 110 | <hr /> |
| 111 | <table width="100%" summary="Navigation footer"> |
| 112 | <tr> |
| 113 | <td width="40%" align="left"><a accesskey="p" href="program_environ.html">Prev</a> </td> |
| 114 | <td width="20%" align="center"> |
| 115 | <a accesskey="u" href="program.html">Up</a> |
| 116 | </td> |
| 117 | <td width="40%" align="right"> <a accesskey="n" href="program_scope.html">Next</a></td> |
| 118 | </tr> |
| 119 | <tr> |
| 120 | <td width="40%" align="left" valign="top">Environment variables </td> |
| 121 | <td width="20%" align="center"> |
| 122 | <a accesskey="h" href="index.html">Home</a> |
| 123 | </td> |
| 124 | <td width="40%" align="right" valign="top"> Berkeley DB handles</td> |
| 125 | </tr> |
| 126 | </table> |
| 127 | </div> |
| 128 | </body> |
| 129 | </html> |