A summary of the use of SQLite multithreading

Source: Internet
Author: User
Tags sqlite
SQLite supports 3 threading modes:
Single-threaded: In this mode, there is no mutual exclusion, and multi-threaded use is not safe. Disable all mutex locks, errors will occur when used concurrently. Enabled when SQLite is compiled with SQLITE_THREADSAFE = 0, or when sqlite3_config (SQLITE_CONFIG_SINGLETHREAD) is called before SQLite is initialized
Multi-threaded: In this mode, as long as one database connection is not used by multiple threads at the same time, it is safe. In the source code, bCoreMutex is enabled and bFullMutex is disabled. In fact, disable the lock on the database connection and prepared statement, so you cannot use the same database connection or prepared statement concurrently in multiple threads. When SQLite is compiled with SQLITE_THREADSAFE = 2, it is enabled by default. If SQLITE_THREADSAFE is not 0, you can enable it by calling sqlite3_config (SQLITE_CONFIG_MULTITHREAD) before initializing SQLite; or set the SQLITE_OPEN_NOMUTEX flag when creating a database connection.
Serial: sqlite is thread-safe. Enable all locks, including bCoreMutex and bFullMutex. Because the database connection and the prepared statement are locked, multi-threaded use of these objects cannot be concurrent and becomes serial. When SQLite is compiled with SQLITE_THREADSAFE = 1, it is enabled by default. If SQLITE_THREADSAFE is not 0, you can enable it by calling sqlite3_config (SQLITE_CONFIG_SERIALIZED) before initializing SQLite; or set the SQLITE_OPEN_FULLMUTEX flag when creating a database connection.
    The initialization mentioned here refers to calling sqlite3_initialize () function, this function will be called automatically when calling sqlite3_open (), and only the first call is valid.
 
   To achieve thread safety, SQLite must set the SQLITE_THREADSAFE preprocessor macro to 1 when compiling. On Windows and Linux, this is set in the compiled good binary distributions. If you are not sure if the library you are using is thread safe, you can find it by calling the sqlite3_threadsafe () interface. Call sqlite3_threadsafe () to get the SQLITE_THREADSAFE parameter at compile time.
 
   In other words, the thread mode can be specified at compile time (when compiling the sqlite library from source), at startup (when an application that uses sqlite is initialized), or at runtime (when creating a database connection). In general, the mode specified at runtime will override the mode specified at startup, and the mode specified at startup will override the mode specified at compile time. However, once single-threaded mode is specified, it cannot be overridden. The default thread mode is serial mode.
 
 
Select thread mode at compile time
    You can specify the thread mode by defining the SQLITE_THREADSAFE macro. If not specified, it defaults to serial mode. Define the macro SQLITE_THREADSAFE = 1 to specify using serial mode; = 0 to use single-threaded mode; = 2 to use multi-threaded mode.
    The return value of the sqlite3_threadsafe () function determines the thread mode specified at compile time. If single-threaded mode is specified, the function returns false. If serial or multi-threaded mode is specified, the function returns true. Since the sqlite3_threadsafe () function is earlier than the multi-threaded mode and the mode selection at startup and runtime, it cannot distinguish between multi-threaded mode and serial mode or between startup and runtime modes.
    The last sentence can be understood through the implementation of the sqlite3_threadsafe function. SQLITE_API int sqlite3_threadsafe (void) {return SQLITE_THREADSAFE;} If single-thread mode is specified at compile time, the critical mutex logic is omitted during construction, so it cannot be used at startup or The runtime specifies serial mode or multi-threaded mode.
 
Select thread mode at startup
    If the single-thread mode is not specified at compile time, you can use the sqlite3_config () function to modify the thread mode during application initialization. The parameter SQLITE_CONFIG_SINGLETHREAD can be specified as
Single-threaded mode, SQLITE_CONFIG_MULTITHREAD is specified as multi-threaded mode, and SQLITE_CONFIG_SERIALIZED is specified as serial mode.
 
Choosing thread mode at runtime
    If you do not specify single-threaded mode at compile time and startup time, each database connection can be individually designated as multi-threaded or serial mode when created, but cannot be specified as single-threaded mode. If you specify single-threaded mode at compile time or startup time, you cannot specify multi-threaded or serial mode when creating a connection.
    When creating a connection, use the third parameter of the sqlite3_open_v2 () function to specify the thread mode. The SQLITE_OPEN_NOMUTEX flag creates a connection in multithreaded mode; the SQLITE_OPEN_FULLMUTEX flag creates a connection in serial mode. If no identity is specified, or if you use the sqlite3_open () or sqlite3_open16 () function to create a database connection, the thread mode specified at compile time or startup time will be used as the default thread mode.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.