#readwise
# Using SQLite in Multi-Threaded Applications

## Metadata
- Author: [[sqlite.org]]
- Full Title: Using SQLite in Multi-Threaded Applications
- URL: https://www.sqlite.org/threadsafe.html
## Summary
SQLite has three threading modes: single-thread, multi-thread, and serialized. Single-thread mode is unsafe for multiple threads, while multi-thread allows safe use if connections are not shared. Serialized mode is safe for multiple threads and uses mutexes to manage access. The threading mode can be set at compile-time, start-time, or run-time, with serialized being the default.
## Highlights
SQLite supports three different threading modes:
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](https://www.sqlite.org/threadsafe.html/c3ref/sqlite3.html) nor any object derived from database connection, such as a [prepared statement](https://www.sqlite.org/threadsafe.html/c3ref/stmt.html), is used in two or more threads at the same time.
3. Serialized. In serialized mode, API calls to affect or use any SQLite [database connection](https://www.sqlite.org/threadsafe.html/c3ref/sqlite3.html) or any object derived from such a database connection can be made safely from multiple threads. The effect on an individual object is the same as if the API calls had all been made in the same order from a single thread. The name "serialized" arises from the fact that SQLite uses mutexes to serialize access to each object. ([View Highlight](https://read.readwise.io/read/01jjhtexr78s34d269ewpevqbw))
---
Assuming that the compile-time threading mode is not single-thread, then the threading mode can be changed during initialization using the [sqlite3_config()](https://www.sqlite.org/c3ref/config.htmll) interface. The [SQLITE_CONFIG_SINGLETHREAD](https://www.sqlite.org/c3ref/c_config_covering_index_scan.html#sqliteconfigsinglethread) verb puts SQLite into single-thread mode, the [SQLITE_CONFIG_MULTITHREAD](https://www.sqlite.org/c3ref/c_config_covering_index_scan.html#sqliteconfigmultithread) verb sets multi-thread mode, and the [SQLITE_CONFIG_SERIALIZED](https://www.sqlite.org/c3ref/c_config_covering_index_scan.html#sqliteconfigserialized) verb sets serialized mode. ([View Highlight](https://read.readwise.io/read/01jjhtg4955eywcxgqx7eem7qd))
---
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. ([View Highlight](https://read.readwise.io/read/01jjhtgqf9jx0jbfhsa314rbqs))
---