SQLite must be configured for the desired threading model before any database handles are opened. These helpers provide a type-safe way to do so.
Use cases
- Call
configure_threadingexactly once during process startup, before any worker threads spawn or connections open. - The default build of SQLite is usually
serialized; explicitly choosingmulti_threadorsingle_threadcan trim mutex overhead in embedded deployments. - Stick with
serializedwhen leveragingconnection_pool—it trades a bit of locking for deadlock-free sharing.
enum class threading_mode
single_thread— disables SQLite's internal mutexes.multi_thread— allows multiple threads, but each connection is single-threaded.serialized— enables full mutexing so one connection can be shared across threads.
Example usage
int main() {
if (!sqlite::configure_threading(sqlite::threading_mode::multi_thread)) {
throw std::runtime_error("Failed to configure SQLite threading");
}
std::jthread worker([] {
sqlite::connection db{":memory:"};
// ...
});
}
I like to expose current_threading_mode() in a health endpoint so it’s easy
to confirm the process bootstrapped SQLite correctly.
Functions
| Function | Description |
|---|---|
bool configure_threading(threading_mode mode) | Calls sqlite3_config with the corresponding SQLITE_CONFIG_* flag. Must be invoked before opening any connections. |
threading_mode current_threading_mode() | Returns the last mode supplied to configure_threading. |