(1)编译阶段

这几种模式可以通过参数SQLITE_THREADSAFE在编译阶段指定,可以取值0,1,2,默认是1。这三种取值的含义如下:

0:单线程模式,即内部不做mutex保护,多线程运行sqlite不安全。

1:多线程的串行模式,sqlite帮助多线程实现串行化。

2:多线程的并发模式,要求同一个时刻,同一个连接不被多个线程使用。

(2)打开数据库阶段

除了可以在编译阶段指定运行模式,还可以在打开数据库时(sqlite3_open_v2())通过参数指定,主要的几个参数以及含义如下:

SQLITE_OPEN_NOMUTEX: 设置数据库连接运行在多线程模式(没有指定单线程模式的情况下)

SQLITE_OPEN_FULLMUTEX:设置数据库连接运行在串行模式。

SQLITE_OPEN_SHAREDCACHE:设置运行在共享缓存模式。

SQLITE_OPEN_PRIVATECACHE:设置运行在非共享缓存模式。

SQLITE_OPEN_READWRITE:指定数据库连接可以读写。

SQLITE_OPEN_CREATE:如果数据库不存在,则创建。

(3)运行时阶段

通过调用sqlite3_config接口,也可以设置运行模式。

若编译参数SQLITE_THREADSAFE=1 or SQLITE_THREADSAFE=2,那么可以在运行时设置线程模式。

SQLITE_CONFIG_SINGLETHREAD:单线程模式

SQLITE_CONFIG_MULTITHREAD:多线程模式,应用层保证同一个时刻,同一个连接只有一个线程使用。

SQLITE_CONFIG_SERIALIZED:串行模式,sqlite帮助多线程实现串行化。

http://www.cnblogs.com/cchust/p/4738002.html

Configuring The SQLite Library

int sqlite3_config(int, ...);

The sqlite3_config() interface is used to make global configuration changes to SQLite in order to tune SQLite to the specific needs of the application. The default configuration is recommended for most applications and so this routine is usually not necessary. It is provided to support rare applications with unusual needs.

The sqlite3_config() interface is not threadsafe. The application must ensure that no other SQLite interfaces are invoked by other threads while sqlite3_config() is running.

The sqlite3_config() interface may only be invoked prior to library initialization using sqlite3_initialize() or after shutdown by sqlite3_shutdown(). If sqlite3_config() is called after sqlite3_initialize() and before sqlite3_shutdown() then it will return SQLITE_MISUSE. Note, however, that sqlite3_config() can be called as part of the implementation of an application-defined sqlite3_os_init().

https://sqlite.org/c3ref/config.html

启动时指定线程模式

  1. int(
  2. filename     sqlite3 ppDb           flags                zVfs        );

在这个函数中,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多线程功能,这几天研究了一下,验证了一下结果,供大家参考:

1、如果是SQLITE_OPEN_FULLMUTEX,也就是串行化方式,则对于连接时互斥的,只有一个连接关闭,另外一个连接才能读写

2、如果是SQLITE_OPEN_NOMUTEX,则是多线程模式,对于写是互斥的,但是如果一个连接持续写,另外一个连接是无法写入的,只能是错误或者超时返回。不过一个连接写,多个连接读,是没问题的。windows版本模式是SQLITE_OPEN_NOMUTEX

---------------------

作者:舟中夜起

来源:CSDN

原文:https://blog.csdn.net/kronus/article/details/6038562

版权声明:本文为博主原创文章,转载请附上博文链接!

The sqlite3_open_v2() interface works like sqlite3_open() except that it accepts two additional parameters for additional control over the new database connection. The flags parameter to sqlite3_open_v2() can take one of the following three values, optionally combined with the SQLITE_OPEN_NOMUTEX, SQLITE_OPEN_FULLMUTEX, SQLITE_OPEN_SHAREDCACHE, SQLITE_OPEN_PRIVATECACHE, and/or SQLITE_OPEN_URI flags:

SQLITE_OPEN_READONLY

The database is opened in read-only mode. If the database does not already exist, an error is returned.

SQLITE_OPEN_READWRITE

The database is opened for reading and writing if possible, or reading only if the file is write protected by the operating system. In either case the database must already exist, otherwise an error is returned.

SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE

The database is opened for reading and writing, and is created if it does not already exist. This is the behavior that is always used for sqlite3_open() and sqlite3_open16().

If the 3rd parameter to sqlite3_open_v2() is not one of the combinations shown above optionally combined with other SQLITE_OPEN_* bits then the behavior is undefined.

If the SQLITE_OPEN_NOMUTEX flag is set, then the database connection opens in the multi-thread threading mode as long as the single-thread mode has not been set at compile-time or start-time.

If the SQLITE_OPEN_FULLMUTEX flag is set then the database connection opens in the serialized threading mode unless single-thread was previously selected at compile-time or start-time.

The SQLITE_OPEN_SHAREDCACHE flag causes the database connection to be eligible to use shared cache mode, regardless of whether or not shared cache is enabled using sqlite3_enable_shared_cache().

The SQLITE_OPEN_PRIVATECACHE flag causes the database connection to not participate in shared cache mode even if it is enabled.

https://sqlite.org/c3ref/open.html

sqlite线程模式的设置的更多相关文章

  1. SQLite 线程安全和并发

    SQLite 与线程 SQLite 是线程安全的. 线程模型 SQLite 支持如下三种线程模型 单线程模型 这种模型下,所有互斥锁都被禁用,同一时间只能由一个线程访问. 多线程模型 这种模型下,一个 ...

  2. COM中的线程模式

      Choosing the threading model for an object depends on the object's function. An object that does e ...

  3. Visual Studio下SQLite数据库开发环境设置

    由于我们介绍的内容都是基于微软的Visual Studio下开发的Win32平台,所以下边我们介绍Visual Studio下SQLite数据库开发环境设置.具体而言我们有两种方式可以在Visual ...

  4. 【原创】System.Data.SQLite内存数据库模式

    对于很多嵌入式数据库来说都有对于的内存数据库模式,SQLite也不例外.内存数据库常常用于极速.实时的场景,一个很好的应用的场景是富客户端的缓存数据,一般富客户端的缓存常常需要分为落地和非落地两种,而 ...

  5. Qt线程QThread简析(8个线程等级,在UI线程里可调用thread->wait()等待线程结束,exit()可直接退出线程,setStackSize设置线程堆栈,首次见到Qt::HANDLE,QThreadData和QThreadPrivate)

    QThread实例代表一个线程,我们可以重新实现QThread::run(),要新建一个线程,我们应该先继承QThread并重新实现run()函数. 需要注意的是: 1.必须在创建QThread对象之 ...

  6. 使用装饰器模式动态设置Drawable的ColorFilter

    使用装饰器模式动态设置Drawable的ColorFilter 欢迎各位关注我的新浪微博:微博 转载请标明出处(kifile的博客) 非常多时候我们都希望Android控件点击的时候,有按下效果,选中 ...

  7. ThreadPoolExecutor线程池参数设置技巧

    一.ThreadPoolExecutor的重要参数   corePoolSize:核心线程数 核心线程会一直存活,及时没有任务需要执行 当线程数小于核心线程数时,即使有线程空闲,线程池也会优先创建新线 ...

  8. 解决搜狗高速模式及设置页面打不开的问题DisableFeature.reg

    搜狗浏览器安装问题1.安装的时候要选择自定义安装,去掉参加用户体验计划的√,否则可能安装不上.2.搜狗sogou_explorer_7.0_0111.exe,设置页面se://settings/?ca ...

  9. 线程池大小设置,CPU的核心数、线程数的关系和区别,同步与堵塞完全是两码事

    线程池应该设置多少线程合适,怎么样估算出来.最近接触到一些相关资料,现作如下总结. 最开始接触线程池的时候,没有想到就仅仅是设置一个线程池的大小居然还有这么多的学问,汗颜啊. 首先,需要考虑到线程池所 ...

随机推荐

  1. [android] 线性布局和布局的组合

    /****************2016年4月25日 更新******************************/ 知乎:对于开发者来说,Android 的开发者选项里有哪些实用的功能? 汤涛 ...

  2. laravel框架详解

    一.基础篇 1.概念 Laravel是一个有着美好前景的年轻框架,它的社区充满着活力,同时提供了完整而清晰的文档,而且为快速.安全地开发现代应用提供了必要的功能.2011年,Taylor Otwell ...

  3. javascript中call()、apply()的区别

    call().apply()的区别: 相同点: 1.call()和apply()都可以用来间接调用函数,都可以显式调用所需的this.即,锚点滑动任何函数可以作为任何对象的方法来调用. 2.两个方法都 ...

  4. K8s helm 创建自定义Chart

    # 删除之前创建的 chart helm list helm delete --purge redis1 # 创建自定义 chart myapp cd ~/helm helm create myapp ...

  5. 将html前端代码提取公因数(5)

    将html前端代码提取公因数(5) 注意:这是优化html代码,对于多个html代码相同的部分提取到一个模板中,只需要编写变化的html 1,利用Django提供的render方法的第三个参数的属性 ...

  6. leetcode-83.删除排序链表中的重复元素

    leetcode-83.删除排序链表中的重复元素 Points 链表 题意 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1- ...

  7. Spring集成ElasticSearch搜索引擎

    目录 前期安装 Maven支持库安装 添加log4j的配置文件 创建Client客户端 实现增删改查以及符合查询 实现查询数据 实现添加数据 实现删除数据 实现修改数据 实现复合查询数据 Elasti ...

  8. recovery 下界面UI旋转90 180 270修改

    原文修改出自简书:https://www.jianshu.com/p/768fdd954061 应该是MTK修改的google源码,支持recovery下屏幕旋转90/180/270, 作者把MTK的 ...

  9. Nginx 安装与部署配置以及Nginx和uWSGI开机自启

    下载 官方网站:https://nginx.org/en/download.html Windows下安装 安装 下载后解压(切记不能含有中文路径!!),文件结构如图(我解压的路径就有中文,记得拷贝放 ...

  10. 产品经理说| AIOps 让告警变得更智能 (下)

    AIOps 人工智能和IT运营支撑 Ops 之间的故事,愈演愈烈,已经成为当今运维圈的热门话题,我打算从2篇文档分享我们在 AIOps 上一些探索和实践.(上篇)主要介绍了为什么事件(告警)处理需要 ...