首页 > 代码库 > sqlite与多线程
sqlite与多线程
数据库支持三种线程模式
Single-thread. In this mode, all mutexes are disabled and SQLite is unsafe to use in more than a single thread at once.
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.
Serialized. In serialized mode, SQLite can be safely used by multiple threads with no restriction.
其中串行模式是默认的。可以在编译时、启动时、运行时指定。运行时会覆盖启动时,启动时会覆盖编译时。
启动时指定线程模式
int sqlite3_open_v2(
const char *filename, /* Database filename (UTF-8) */
sqlite3 **ppDb, /* OUT: SQLite db handle */
int flags, /* Flags */
const char *zVfs /* Name of VFS module to use */
);
在这个函数中,flags可以指定线程模式,还可以指定其他的配置。
flags的第一部分必须是下面三个之一:
1. SQLITE_OPEN_READONLY
2. SQLITE_OPEN_READWRITE
3. SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
第二部分可以选择 SQLITE_OPEN_NOMUTEX, SQLITE_OPEN_FULLMUTEX,SQLITE_OPEN_SHAREDCACHE,SQLITE_OPEN_PRIVATECACHE或 SQLITE_OPEN_URI。
- SQLITE_OPEN_NOMUTEX,选择multi-thread线程模式
- SQLITE_OPEN_FULLMUTEX,进入serialized线程模式。
sqlite与多线程