在这种情况下,SQLite线程安全吗? [英] Is SQLite thread safe under this situation?

查看:119
本文介绍了在这种情况下,SQLite线程安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过一个拥有数据库连接的单例对象从多个线程进行数据库访问操作.我从SQLite3的网站上读到,说'一个sqlite3结构只能在调用sqlite3_open()创建该线程的同一线程中使用.您无法在一个线程中打开数据库,然后将句柄传递给另一个线程以供使用".我想知道在我的情况下,是否存在线程不安全的威胁?

解决方案

如果使用-DSQLITE_THREADSAFE编译SQLite库,则可以使用较新的SQLite 3版本.

SQLite的作者:

从版本3.5.0开始,SQLite会使用其
强制执行此操作 自己的内部互斥锁,因此应用程序可以自由地(尝试)使用
来自多个线程的相同数据库连接.

I require database access operations from several threads, through a singleton object, which holds a database connection. I read from SQLite3's website, saying that 'an sqlite3 structure could only be used in the same thread that called sqlite3_open() to create it. You could not open a database in one thread then pass the handle off to another thread for it to use'. I'm wondering under my situation, is there any threat of thread-unsafety?

解决方案

If the SQLite library is compiled with -DSQLITE_THREADSAFE you are OK with the more recent SQLite 3 versions.

The author of SQLite says:

Beginning with version 3.5.0, SQLite enforces this itself using its
own internal mutexes, so the application is free to (try to) use the
same database connection from multiple threads at the same time.

这篇关于在这种情况下,SQLite线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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