sqlite in-memory db and multithreading

淺唱寂寞╮ 提交于 2020-01-01 15:33:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!