1)单线程single-thread。这种模式中,所有的互斥锁都失效,并且sqlite超过一个线程就不能安全的使用。
2)多线程multi-thread。这种模式中,sqlite可以安全的被多个线程使用,假如此时在两个或者更多线程中同时使用不止一个database的连接。只要此时没有两个或者更多的线程同时使用一个database的连接。
3)串行化serialized。在串行化模式中,sqlite可以不受限制的使用安全的被多个线程使用。
线程模式可以在编译阶段(当sqlite库从源码中进行编译时),或者在启动阶段(当准备使用sqlite的应用程序正在初始化时),或者运行阶段(当新的sqlite数据库连接正在被创建时)。一般上来讲,运行阶段可以覆盖启动阶段,而启动阶段可以覆盖编译阶段。除了,单线程模式一旦选定就不能被覆盖更改。
默认模式是串行化模式。
编译阶段线程模式的选择
使用SQLITE_THREADSAFE的编译阶段参数来选择线程模式。如果不存在SQLITE_THREADSAFE的编译阶段参数,那么使用serialized mode。可以通过使用-DSQLITE_THREADSAFE=1显式的指定。使用-DSQLITE_THREADSAFE=1则线程模式是single-thread.使用-DSQLITE_THREADSAFE=2 则线程模式是multi-thread.
sqlite3_threadsafe()接口的返回值是由编译阶段线程模式的选择来决定的。如果在编译阶段选择了single-thread模式,那么sqlite3_threadsafe()返回false。如果选择的是multi-thread或者serialized,那么sqlite3_threadsafe()返回true。sqlite3_threadsafe()接口早于multi-thread模式和启动阶段以及运行阶段的模式选择,所以不能区分multi-thread和serialized,也不能报告通知启动阶段和运行阶段模式的变更。
如果single-thread模式在编译阶段被选择了,那么关键的互斥逻辑在构建时就被省略掉了,也不可能在启动阶段或者运行阶段让multi-thread和serialized使能。
启动阶段线程模式的选择
假设编译阶段线程模型不是single-thread,那么线程模型就可以使用sqlite3_config() 接口在初始化过程中更改。SQLITE_CONFIG_SINGLETHREAD动词将sqlite置于单线程模式,而SQLITE_CONFIG_MULTITHREAD则职位多线程模式,SQLITE_CONFIG_SERIALIZED是serialized模式。
运行阶段线程模式的选择
如果在编译阶段或者启动阶段线程模型选择的不是single-thread,那么单个的数据库连接可以以multi-thread和serialized创建。但是将一个独立的数据库连接降级成single-thread模式是不可能的。也不能再编译阶段或者启动阶段模式是single-thread时将一个单独的数据库连接的模式增强。
一个单独的数据库的线程模式是由sqlite3_open_v2()的第三个参数中的flags给出决定的。SQLITE_OPEN_NOMUTEX的标志将使得数据库连接处于multi-thread,而SQLITE_OPEN_FULLMUTEX标志则将连接处于serialized模式。如果没有flag被指定或者没有使用sqlite3_open_v2()而是sqlite3_open() or sqlite3_open16(),那么默认使用的模式将由编译阶段和启动阶段的设置来决定。
sqlite And Multiple Threads
sqlite support three different threading modes:
-
Single-thread. In this mode,all mutexes are disabled and sqlite is unsafe to use in more than a single thread at once.
-
Multi-thread. In this mode,sqlite can be safely used by multiple threads provided that no single database connection is used simultaneously in two or more threads.
-
Serialized. In serialized mode,sqlite can be safely used by multiple threads with no restriction.
The threading mode can be selected at compile-time (when the sqlite library is being compiled from source code) or at start-time (when the application that intends to use sqlite is initializing) or at run-time (when a new sqlite database connection is being created). Generally speaking,run-time overrides start-time and start-time overrides compile-time. Except,single-thread mode cannot be overridden once selected.
The default mode is serialized.
Compile-time selection of threading mode
Use the SQLITE_THREADSAFE compile-time parameter to selected the threading mode. If noSQLITE_THREADSAFE compile-time parameter is present,then serialized mode is used. This can be made explicit with-DSQLITE_THREADSAFE=1. With-DSQLITE_THREADSAFE=0 the threading mode is single-thread. With-DSQLITE_THREADSAFE=2 the threading mode is multi-thread.
The return value of the sqlite3_threadsafe() interface is determined by the compile-time threading mode selection. If single-thread mode is selected at compile-time,thensqlite3_threadsafe() returns false. If either the multi-thread or serialized modes are selected,thensqlite3_threadsafe() returns true. Thesqlite3_threadsafe() interface predates the multi-thread mode and start-time and run-time mode selection and so is unable to distinguish between multi-thread and serialized mode nor is it able to report start-time or run-time mode changes.
If single-thread mode is selected at compile-time,then critical mutexing logic is omitted from the build and it is impossible to enable either multi-thread or serialized modes at start-time or run-time.
Start-time selection of threading mode
Assuming that the compile-time threading mode is not single-thread,then the threading mode can be changed during initialization using thesqlite3_config() interface. TheSQLITE_CONFIG_SINGLETHREAD verb puts sqlite into single-thread mode,the SQLITE_CONFIG_MULTITHREAD verb sets multi-thread mode,and the SQLITE_CONFIG_SERIALIZED verb sets serialized mode.
Run-time selection of threading mode
If single-thread mode has not been selected at compile-time or start-time,then individual database connections can be created as either multi-thread or serialized. It is not possible to downgrade an individual database connection to single-thread mode. Nor is it possible to escalate an individual database connection if the compile-time or start-time mode is single-thread.
The threading mode for an individual database connection is determined by flags given as the third argument tosqlite3_open_v2(). TheSQLITE_OPEN_NOMUTEX flag causes the database connection to be in the multi-thread mode and theSQLITE_OPEN_FULLMUTEX flag causes the connection to be in serialized mode. If neither flag is specified or ifsqlite3_open() orsqlite3_open16() are used instead ofsqlite3_open_v2(),then the default mode determined by the compile-time and start-time settings is used.