首页 > 解决方案 > 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?

标签: c++sqlite

解决方案


  1. “我的程序中有很多线程,”这表明您最好重新考虑您的设计。你目前的设计会一次又一次地绊倒你。但是,使用您当前的设计:

  2. 您需要为每个查询创建一个新的 sqlite3_stmt。将其从静态更改为自动,因此每次调用 db_query(int id) 时都会创建它。

  3. 您不能“同时”运行查询。你必须一次做一个。因此,您需要使用互斥锁保护您的查询代码。


推荐阅读