1

I'm trying to set single-threaded mode on start-time in a program using multi-thread compiled SQLite. Here's the code:

#include <iostream>
#include <sqlite3.h>

using namespace std;

int main() {
  cout << "threadsafe: " << sqlite3_threadsafe() << endl;
  int mode = SQLITE_CONFIG_SINGLETHREAD;
  sqlite3_shutdown();
  int res = sqlite3_config(mode);
  if (res) {
    cout << "failed to set threading mode, error code: " + to_string(res);
  } else {
    cout << "threading mode successfully set to " + to_string(mode);
  }
  sqlite3_initialize();

  cout << endl;
  return 0;
}

// command used to compile and run
// g++ repro.cpp -o repro.out -lsqlite3 && ./repro.out

The output is:

threadsafe: 2
failed to set threading mode, error code: 21

Now, from what I understand from here and other SQLite docs, the compile-time mode determines whether the mutexes code exists in the compiled files of SQLite. On the other hand, start-time and run-time settings either tell the program to use/omit mutexes code(if it's present obviously). Now, if I get 2 as a result of sqlite3_threadsafe() call, it means the SQLite code has been compiled in the multi-threaded mode. That means that the mutex code is compiled and available. However, in order to care for the performance, I'd like to tell the program to skip the mutexes code as I'm going to use SQLite only on one thread. To do this, I followed the docs and used sqlite3_config() with the option SQLITE_CONFIG_SINGLETHREAD. This didn't work and I got the result 21 from the call. The error code means SQLITE_MISUSE. I got to this SO answer so I put the config call between sqlite3_shutdown andsqlite3_initialize. This didn't work.

I'm not sure why this doesn't work, in the linked doc they say:

It is not possible to downgrade an individual database connection to single-thread mode

But it's about an individual connection and I'm trying to get this working for all of the SQLite calls in the program. I couldn't find an answer on the docs or the Internet. Am I missing something?

EDIT: sorry, forgot to put versions:

  • mac os: 11.2.3
  • Apple clang version 12.0.5
  • sqlite3: 3.32.3
Karol
  • 23
  • 4
  • You usually don't need to ever use `sqlite3_initialize()` or `sqlite3_shutdown()`. Just call `sqlite3_config()` before opening a database or other sqlite functions. – Shawn May 29 '21 at 00:46
  • Hint: Use `sqlite3_errstr()` to convert a numeric code to a human-readable error string. – Shawn May 29 '21 at 00:48
  • This doesn't work without those two functions either. `sqlite3_errstr()` shows `bad parameter or other API misuse` so it doesn't really give me anything I wouldn't know, doesn't point out a problem :/ But thanks for the tips. – Karol May 31 '21 at 06:16

0 Answers0