sqlite内存数据库和多线程 [英] sqlite in-memory db and multithreading

查看:746
本文介绍了sqlite内存数据库和多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用sqlite作为后端创建一个内存数据库(:memory :)。



我想我的主线程创建一个内存数据库的连接,并且这个连接被多个线程共享。这可能吗? SQLite 3.7.8现在可以下载。



共享缓存可能

如果使用序列化模式,则可以在多个线程之间共享连接。



为了使其正常工作,您的SQLite必须编译线程安全 - 这是默认值。



根据您的应用程序,您可以使用磁盘数据库的大型共享缓存获得更好的性能,如果您有很多读取线程,则可以使用WAL模式。



示例:

  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);

//等
}


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?

解决方案

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.
}

这篇关于sqlite内存数据库和多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆