(1)編譯階段
這幾種模式可以通過參數SQLITE_THREADSAFE在編譯階段指定,可以取值0,1,2,默認是1。這三種取值的含義如下:
0:單線程模式,即內部不做mutex保護,多線程運行sqlite不安全。
1:多線程的串行模式,sqlite幫助多線程實現串行化。
2:多線程的並發模式,要求同一個時刻,同一個連接不被多個線程使用。
(2)打開數據庫階段
除了可以在編譯階段指定運行模式,還可以在打開數據庫時(sqlite3_open_v2())通過參數指定,主要的幾個參數以及含義如下:
SQLITE_OPEN_NOMUTEX: 設置數據庫連接運行在多線程模式(沒有指定單線程模式的情況下)
SQLITE_OPEN_FULLMUTEX:設置數據庫連接運行在串行模式。
SQLITE_OPEN_SHAREDCACHE:設置運行在共享緩存模式。
SQLITE_OPEN_PRIVATECACHE:設置運行在非共享緩存模式。
SQLITE_OPEN_READWRITE:指定數據庫連接可以讀寫。
SQLITE_OPEN_CREATE:如果數據庫不存在,則創建。
(3)運行時階段
通過調用sqlite3_config接口,也可以設置運行模式。
若編譯參數SQLITE_THREADSAFE=1 or SQLITE_THREADSAFE=2,那么可以在運行時設置線程模式。
SQLITE_CONFIG_SINGLETHREAD:單線程模式
SQLITE_CONFIG_MULTITHREAD:多線程模式,應用層保證同一個時刻,同一個連接只有一個線程使用。
SQLITE_CONFIG_SERIALIZED:串行模式,sqlite幫助多線程實現串行化。
http://www.cnblogs.com/cchust/p/4738002.html
Configuring The SQLite Library
int sqlite3_config(int, ...);
The sqlite3_config() interface is used to make global configuration changes to SQLite in order to tune SQLite to the specific needs of the application. The default configuration is recommended for most applications and so this routine is usually not necessary. It is provided to support rare applications with unusual needs.
The sqlite3_config() interface is not threadsafe. The application must ensure that no other SQLite interfaces are invoked by other threads while sqlite3_config() is running.
The sqlite3_config() interface may only be invoked prior to library initialization using sqlite3_initialize() or after shutdown by sqlite3_shutdown(). If sqlite3_config() is called after sqlite3_initialize() and before sqlite3_shutdown() then it will return SQLITE_MISUSE. Note, however, that sqlite3_config() can be called as part of the implementation of an application-defined sqlite3_os_init().
https://sqlite.org/c3ref/config.html
啟動時指定線程模式
- int(
- filename sqlite3 ppDb flags zVfs );
在這個函數中,flags可以指定線程模式,還可以指定其他的配置。
flags的第一部分必須是下面三個之一:
1. SQLITE_OPEN_READONLY
2. SQLITE_OPEN_READWRITE
3. SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
第二部分可以選擇 SQLITE_OPEN_NOMUTEX, SQLITE_OPEN_FULLMUTEX,SQLITE_OPEN_SHAREDCACHE,SQLITE_OPEN_PRIVATECACHE或 SQLITE_OPEN_URI。
- SQLITE_OPEN_NOMUTEX,選擇multi-thread線程模式
- SQLITE_OPEN_FULLMUTEX,進入serialized線程模式。
這幾工作需要,用到sqlite多線程功能,這幾天研究了一下,驗證了一下結果,供大家參考:
1、如果是SQLITE_OPEN_FULLMUTEX,也就是串行化方式,則對於連接時互斥的,只有一個連接關閉,另外一個連接才能讀寫
2、如果是SQLITE_OPEN_NOMUTEX,則是多線程模式,對於寫是互斥的,但是如果一個連接持續寫,另外一個連接是無法寫入的,只能是錯誤或者超時返回。不過一個連接寫,多個連接讀,是沒問題的。windows版本模式是SQLITE_OPEN_NOMUTEX
---------------------
作者:舟中夜起
來源:CSDN
原文:https://blog.csdn.net/kronus/article/details/6038562
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!
The sqlite3_open_v2() interface works like sqlite3_open() except that it accepts two additional parameters for additional control over the new database connection. The flags parameter to sqlite3_open_v2() can take one of the following three values, optionally combined with the SQLITE_OPEN_NOMUTEX, SQLITE_OPEN_FULLMUTEX, SQLITE_OPEN_SHAREDCACHE, SQLITE_OPEN_PRIVATECACHE, and/or SQLITE_OPEN_URI flags:
The database is opened in read-only mode. If the database does not already exist, an error is returned.
The database is opened for reading and writing if possible, or reading only if the file is write protected by the operating system. In either case the database must already exist, otherwise an error is returned.
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
The database is opened for reading and writing, and is created if it does not already exist. This is the behavior that is always used for sqlite3_open() and sqlite3_open16().
If the 3rd parameter to sqlite3_open_v2() is not one of the combinations shown above optionally combined with other SQLITE_OPEN_* bits then the behavior is undefined.
If the SQLITE_OPEN_NOMUTEX flag is set, then the database connection opens in the multi-thread threading mode as long as the single-thread mode has not been set at compile-time or start-time.
If the SQLITE_OPEN_FULLMUTEX flag is set then the database connection opens in the serialized threading mode unless single-thread was previously selected at compile-time or start-time.
The SQLITE_OPEN_SHAREDCACHE flag causes the database connection to be eligible to use shared cache mode, regardless of whether or not shared cache is enabled using sqlite3_enable_shared_cache().
The SQLITE_OPEN_PRIVATECACHE flag causes the database connection to not participate in shared cache mode even if it is enabled.
https://sqlite.org/c3ref/open.html