Although SQLite is thread safe, you still cannot modify the database at the same time:
Then each thread inserts the number of records, say 1000. The problem you will encounter is this: one thread will gain control of the database by setting a lock on the file. This is fine, but the rest of the threads will continue to fail for each INSERT attempt, while the lock is active. ( link )
Only one thread can modify the database at a time, but you can have multiple threads trying to modify the database.
If you want to avoid the lock delay problem, you can check the SQLITE_BUSY flag:
Test for SQLITE_BUSY, which I did not use initially. Here is some pseudo code to illustrate the solution:
while (continueTrying) { retval = sqlite_exec(db, sqlQuery, callback, 0, &msg); switch (retval) { case SQLITE_BUSY: Log("[%s] SQLITE_BUSY: sleeping fow a while...", threadName); sleep a bit... (use something like sleep(), for example) break; case SQLITE_OK: continueTrying = NO; // We're done break; default: Log("[%s] Can't execute \"%s\": %s\n", threadName, sqlQuery, msg); continueTrying = NO; break; } } return retval;
same link
My bet is that breaking your restriction has nothing to do with a multi-threaded process, so could you please post the actual restriction violation you get (or an example that matches www.sscce.org ).
Kiril
source share