问题
My application creates a in-memory database (:memory:) using sqlite as a back end.
I want my master thread to create a connection to a in-memory database and this connection to be shared by multiple threads. Is this possible? SQLite 3.7.8 is available for download right now.
Is the shared cached a possible way to go?
回答1:
If you open the connection to your in-memory database using serialized mode, then the connection may be shared among multiple threads.
For this to work, your SQLite must be compiled threadsafe -- this is the default.
Depending on your application, you may get better performance with a large shared cache to an on-disk database, or with WAL mode if you have many reader threads.
Example:
sqlite3 *pDb
if (sqlite3_open_v2(":memory:", &pDb, SQLITE_OPEN_FULLMUTEX, NULL) == SQLITE_OK) {
start_thread1_with_db_handle(pDb);
start_thread2_with_db_handle(pDb);
// etc.
}
来源:https://stackoverflow.com/questions/7945514/sqlite-in-memory-db-and-multithreading