dimanche 23 juillet 2017

SQLite - multithread read and write in C++

I have C++11 app, that uses multiple threads. Each thread can read or write database, eg. doing INSERT, UPDATE, DELETE, SELECT. I have enabled serialized mode for SQLite, so connection can be shared between threads.

However, I dont know, how to run queries. Can I just run single query and create statement via sqlite3_prepare_v2? Or, should I add my own locks via std::lock_guard<std::mutex> and do something like:

Thread #1
db.lock()
db.query("....").execute()
db.unlock()

Thread #2
db.lock()
res = db.query("....").select()
while(res) res.row()
db.unlock()

Or is there any other way? I have been looking for some sample code, but found nothing.

Aucun commentaire:

Enregistrer un commentaire