Sqlite multithreading for single threaded mode with multiple databases

Sqlite documentation says multithreading

1. Single-thread. In this mode, all mutexes are disabled and SQLite is unsafe to use in more than a single thread at once. 2. 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. 3. Serialized. In serialized mode, SQLite can be safely used by multiple threads with no restriction. 

I have a question about 1 (single thread mode). Does it say

  • You cannot use sqlite3_open in two threads.

  • This prevents using sqlite3_open to open the same database in two thread.But use sqlite3_open to open two different databases in two threads in order.

+4
source share
1 answer

Option 1. If you need option 2, you need to use multi-threaded mode, just like the text says. In single-threaded mode, the global state of sqlite functions will not be protected, and using them simultaneously in multiple threads will be a problem.

In multithreaded mode, you can use sqlite in multiple threads in one application, but you cannot share the same connection between threads at the same time.

+2
source

Source: https://habr.com/ru/post/1486396/


All Articles