📄 threadsafe.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><title>Using SQLite In Multi-Threaded Applications</title><style type="text/css">body { margin: auto; font-family: "Verdana" "sans-serif"; padding: 8px 1%;}a { color: #45735f }a:visited { color: #734559 }.logo { position:absolute; margin:3px; }.tagline { float:right; text-align:right; font-style:italic; width:240px; margin:12px; margin-top:58px;}.toolbar { font-variant: small-caps; text-align: center; line-height: 1.6em; margin: 0; padding:1px 8px;}.toolbar a { color: white; text-decoration: none; padding: 6px 12px; }.toolbar a:visited { color: white; }.toolbar a:hover { color: #80a796; background: white; }.content { margin: 5%; }.content dt { font-weight:bold; }.content dd { margin-bottom: 25px; margin-left:20%; }.content ul { padding:0px; padding-left: 15px; margin:0px; }/* rounded corners */.se { background: url(images/se.png) 100% 100% no-repeat #80a796}.sw { background: url(images/sw.png) 0% 100% no-repeat }.ne { background: url(images/ne.png) 100% 0% no-repeat }.nw { background: url(images/nw.png) 0% 0% no-repeat }</style><meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head><body><div><!-- container div to satisfy validator --><a href="index.html"><img class="logo" src="images/SQLite.gif" alt="SQLite Logo" border="0"></a><div><!-- IE hack to prevent disappearing logo--></div><div class="tagline">Small. Fast. Reliable.<br>Choose any three.</div><table width=100% style="clear:both"><tr><td> <div class="se"><div class="sw"><div class="ne"><div class="nw"> <div class="toolbar"> <a href="about.html">About</a> <a href="sitemap.html">Sitemap</a> <a href="docs.html">Documentation</a> <a href="download.html">Download</a> <a href="copyright.html">License</a> <a href="news.html">News</a> <a href="http://www.sqlite.org/cvstrac/index">Developers</a> <a href="support.html">Support</a> </div></div></div></div></div></td></tr></table> <h2>SQLite And Multiple Threads</h2><p>SQLite support three different threading modes:</p><ol><li><p><b>Single-thread</b>.In this mode, all mutexes are disabled and SQLite is unsafe to use inmore than a single thread at once.</p></li><li><p><b>Multi-thread</b>.In this mode, SQLite can be safely used by multiple threads provided thatno single database connection is used simulataneously in two or more threads.</p></li><li><p><b>Serialized</b>.In serialized mode, SQLite can be safely used by multiple threads with norestriction.</p></li></ol><p>The threading mode can be selected at compile-time (when the SQLitelibrary is being compiled from source code) or at start-time (when theapplication that intends to use SQLite is initializing) or atrun-time (when a new SQLite database connection is being created).Generally speaking, run-time overrides start-time and start-timeoverrides compile-time. Except, single-thread mode cannot be overridden once selected.</p><p>The default mode is serialized.</p><h3>Compile-time selection of threading mode</h3><p>Use the <a href="compile.html#threadsafe">SQLITE_THREADSAFE</a> compile-time parameter to selected thethreading mode. If no <a href="compile.html#threadsafe">SQLITE_THREADSAFE</a> compile-time parameter ispresent, then serialized mode is used.This can be made explicit with <a href="compile.html#threadsafe">-DSQLITE_THREADSAFE=1</a>.With<a href="compile.html#threadsafe">-DSQLITE_THREADSAFE=0</a> the threading mode issingle-thread. With<a href="compile.html#threadsafe">-DSQLITE_THREADSAFE=2</a> the threading mode ismulti-thread.</p><p>The return value of the <a href="c3ref/threadsafe.html">sqlite3_threadsafe()</a> interface is determinedby the compile-time threading mode selection. If single-thread modeis selected at compile-time, then <a href="c3ref/threadsafe.html">sqlite3_threadsafe()</a> returns false.If either the multi-thread or serialized modes are selected, then<a href="c3ref/threadsafe.html">sqlite3_threadsafe()</a> returns true. The <a href="c3ref/threadsafe.html">sqlite3_threadsafe()</a>interface predates the multi-thread mode and start-time and run-timemode selection and so is unable to distinguishbetween multi-thread and serialized mode nor is it able to report start-timeor run-time mode changes.</p><p>If single-thread mode is selected at compile-time, then criticalmutexing logic is omitted from the build and it is impossible toenable either multi-thread or serialized modes at start-time orrun-time.</p><h3>Start-time selection of threading mode</h3><p>Assuming that the compile-time threading mode is not single-thread, thenthe threading mode can be changed during initialization using the<a href="c3ref/config.html">sqlite3_config()</a> interface. The <a href="c3ref/c_config_getmalloc.html">SQLITE_CONFIG_SINGLETHREAD</a> verbputs SQLite into single-thread mode, the <a href="c3ref/c_config_getmalloc.html">SQLITE_CONFIG_MULTITHREAD</a>verb sets multi-thread mode, and the <a href="c3ref/c_config_getmalloc.html">SQLITE_CONFIG_SERIALIZED</a> verbsets serialized mode.</p><h3>Run-time selection of threading mode</h3><p>If single-thread mode has not been selected at compile-time or start-time,then individual database connections can be created as either multi-threador serialized. It is not possible to downgrade an individual databaseconnection to single-thread mode. Nor is it possible to escalate anindividual database connection if the compile-time or start-time modeis single-thread.</p><p>The threading mode for an individual database connection is determinedby flags given as the third argument to <a href="c3ref/open.html">sqlite3_open_v2()</a>. The<a href="c3ref/c_open_create.html">SQLITE_OPEN_NOMUTEX</a> flag causes the database connection to be in themulti-thread mode and the <a href="c3ref/c_open_create.html">SQLITE_OPEN_FULLMUTEX</a> flag causes the connectionto be in serialized mode. If neither flag is specified or if<a href="c3ref/open.html">sqlite3_open()</a> or <a href="c3ref/open.html">sqlite3_open16()</a> are used instead of <a href="c3ref/open.html">sqlite3_open_v2()</a>, then the defaultmode determined by the compile-time and start-time settings is used.</p><hr><small><i>This page last modified 2008/10/10 16:11:25 UTC</i></small></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -