標籤:
資料庫支援三種線程模式
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與多線程