Skip to main content
 首页 » 编程设计

用于具有多个数据库的单线程模式的 sqlite 多线程

2025年05月04日96cyq1162

Sqlite 文档说明了多线程

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. 

我有一个关于 1(单线程模式)的问题。这是否说
  • 它不允许在两个线程中使用 sqlite3_open。
  • 不允许使用 sqlite3_open 分两次打开同一个数据库
    线程。但是使用 sqlite3_open 在两个线程中打开两个不同的数据库是可以的。
  • 请您参考如下方法:

    选项一。如果你想要选项二,你需要使用多线程模式,正如文中所说。在单线程模式下,sqlite 函数的全局状态不会受到保护,并且在多个线程中同时使用它们将是一个问题。

    使用多线程模式,您可以在同一个应用程序的多个线程中使用 sqlite,但您不能同时在线程之间共享单个连接。