c++ - what is a simple way to run many similar queries concurrently in SQLite in c++?
问题描述
There are many threads in my program, and they simply run queries like "select content from table where id= xxx".
I first planned to provide a db_query(int id)
function with a static sqlite3
object and a static sqlite3_stmt
object which stands for the query. So all the threads can call this function and get results.
But then I find that the sqlite3_stmt
object is not stateless and cannot be used by many threads. In addition, there seems not a function provided for copying a sqlite3_stmt
object, so I cannot just make a copy of the prepared statement in each function call.
Is there an elite and easy-to-implement way to solve my problem?
解决方案
“我的程序中有很多线程,”这表明您最好重新考虑您的设计。你目前的设计会一次又一次地绊倒你。但是,使用您当前的设计:
您需要为每个查询创建一个新的 sqlite3_stmt。将其从静态更改为自动,因此每次调用 db_query(int id) 时都会创建它。
您不能“同时”运行查询。你必须一次做一个。因此,您需要使用互斥锁保护您的查询代码。
推荐阅读
- javascript - 带有嵌套图标的iOS画外音html按钮不执行单击双击
- c++ - 在 C++ 中,用自身初始化全局变量是否具有未定义的行为?
- android - Google 地图 - 您应用的 AndroidManifest.xml 中的必需元数据标签不存在
- flutter - Flutter 将样式应用到子小部件是否有必要将其包装在 Container Widget 中
- python - 如何在另一个线程中设置 qasync 事件循环?
- vagrant - 我在 islandora-playbook 存储库上流浪时遇到了这两个错误,你对这个问题有什么想法吗?
- java - 反序列化 GSON 中的字符串列表?
- installation - 如何以我已有的路径之间没有冲突的方式安装 Anaconda?
- c - 如何对指向有名字的地方的 void* 数组进行排序?
- zos - z/OS Unix 系统服务 BPXBATCH 以 CC=3840 结束