sqlite threading model

Official website: https://www.sqlite.org/c3ref/open.html

Reprinted: https://blog.csdn.net/yifanernei/article/details/5642127

Thread SQLite supports three modes:
1. the single thread mode
    In this mode, no exclusive, multithreading unsafe
2. The multi-thread mode
    In this mode, a single database connection is multithreading unsafe, otherwise, it is safe. (Translation: a plurality of threads can not be shared database connections)
3. Serial Mode
    In this mode, sqlite be thread-safe. (Annotation: without even mutually exclusive in the plurality of threads using the same database connection)

 (When using sqlite application initialization) Thread mode (when compiled from source sqlite library), start at compile time or runtime (create a database connection) is specified. In general, the specified runtime mode overrides the specified mode at startup, specified at startup mode will override the compile-time mode. However, once the single-threaded mode is specified, it can not be overwritten.
    The default threading model is the serial mode.

 

Select the threading model compile time
    by defining SQLITE_THREADSAFE macro to specify the threading model. If not specified, the default is the serial mode. Macros defined SQLITE_THREADSAFE = 1 specify serial mode; 0 = single-threaded mode; 2 = multi-thread mode.
    sqlite3_threadsafe () function returns the value of the specified thread mode may be determined at compile time. If a single-threaded mode is specified, the function returns false. If a serial or multi-threaded mode is specified, the function returns true. Since sqlite3_threadsafe () function mode earlier than the multi-thread mode and run-time mode and select the startup, so both can not distinguish multi-thread mode and serial mode can not distinguish startup and runtime.

Annotation: The last sentence can be understood by implementing sqlite3_threadsafe function

SQLITE_API int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }

 

   If you specify a single-threaded mode compile time, so it was critical logic exclusive omitted when constructing and, therefore, can not be specified serial mode or multi-threaded mode at startup or runtime.

 

Select the thread mode when you start
    if you do not specify single-threaded mode at compile time, you can use sqlite3_config when the application is initialized () function to modify thread mode. SQLITE_CONFIG_SINGLETHREAD parameters can be specified as single-threaded mode, SQLITE_CONFIG_MULTITHREAD designated for multi-threaded mode, SQLITE_CONFIG_SERIALIZED designated as a serial mode.

Select the thread mode when
    designated as single-threaded mode when not at compile time and start, then each database connection when you create can be designated as a separate multi-threaded mode or serial mode, but can not be specified as a single-threaded mode. If specified at compile time or start a single-threaded mode, you can not specify a multi-threaded or serial mode when creating the connection.
    Thread mode is specified when creating a connection with sqlite3_open_v2 () third argument to the function. SQLITE_OPEN_NOMUTEX logo create multi-threaded mode of connection; SQLITE_OPEN_FULLMUTEX identify the mode of creating a serial connection. If you do not specify an identity, or use sqlite3_open () or sqlite3_open16 () function to create a database connection, specified at compile time or start threading model will use as the default threading model.

Only opened a multi-threaded model, in order to compete in a multi-threaded database occur in use sqlite3_busy_handler () function to retry

 

Guess you like

Origin www.cnblogs.com/chechen/p/10956896.html