Small. Fast. Reliable.
Choose any three.
*** 2,24 ****
  
  Keep in mind, however, that I will have left things out. If you happen to discover something about multithreading that isn't covered here, feel free to add it. Some of the references in this paper are Mac OS X related because this is the environment I've been working on.
  
  **SQLite multithreading settings**
  
! The setting named *DTHREADSAFE* turns multithreading on or off. In Windows, this setting is turned on by default. Under Mac OS X, you'll have to set it manually. In Project Builder, you can easily turn on multithreading by adding *-DTHREADSAFE=1* to the *Other C Compiler Flags* field, in *Targets*:
  
! _<your project>:Settings:GCC Compiler Settings_
  
! Some messages in the SQLite group list refer to the following functions: *sqliteOsEnterMutex()* and *sqliteOsLeaveMutex()*. These functions set and clear the mutex lock, which is needed to guarantee a thread-safe environment. Under Mac OS X and Windows, the *sqliteOsEnterMutex()* and *sqliteOsLeaveMutex()* functions are already implemented in os.c.
  
  **Study case: multithreaded insert on the same database**
  
  *:  Spawn two or more threads. Each one, opens the db via *sqlite_open()* and keeps its own copy of sqlite structure.
  
  *:  Each thread then proceeds to insert a number of records, let's say 1000. The problem you will encounter is the following: one thread will get control over the database by setting a lock on the file. This is fine, but the rest of the threads will keep on failing for each attempted *INSERT* _while_ the lock is active.
  
  **Solution**
  
! Test for *SQLITE_BUSY*, which I didn't do originally. Here's some pseudo-code to illustrate a solution:
  
    while (continueTrying) {
      retval = sqlite_exec(db, sqlQuery, callback, 0, &msg);
--- 2,30 ----
  
  Keep in mind, however, that I will have left things out. If you happen to discover something about multithreading that isn't covered here, feel free to add it. Some of the references in this paper are Mac OS X related because this is the environment I've been working on.
  
+ **Is SQLite thread-safe?**
+ 
+ Yes, it is, but be {link: http://www.hwaci.com/sw/sqlite/faq.html#q8 careful}.
+ 
  **SQLite multithreading settings**
  
! The setting named {link: http://groups.yahoo.com/group/sqlite/message/440 DTHREADSAFE} turns multithreading on or off. {link: http://groups.yahoo.com/group/sqlite/message/527 It's turned on by default in the precompiled Windows binaries and it's off by default in the precompiled Linux binaries}. Under Linux, Mac OS X and other Unix systems, you'll have to set it manually. If you're using Mac OS X's {link: http://developer.apple.com/tools/projectbuilder/index.html Project Builder}, you can easily turn on multithreading by adding *-DTHREADSAFE=1* to the *Other C Compiler Flags* field, in the following panel:
  
! _Project:Edit active target '<your project>':Settings:GCC Compiler Settings_
  
! Some messages in the SQLite group list refer to the following functions: *sqliteOsEnterMutex()* and *sqliteOsLeaveMutex()*. These functions set and clear the mutex lock, which is needed to guarantee a thread-safe environment. Under Mac OS X and Windows, the *sqliteOsEnterMutex()* and *sqliteOsLeaveMutex()* functions are already implemented in *os.c*.
  
  **Study case: multithreaded insert on the same database**
  
+ _If you're new to SQLite, take a quick look at this {link: http://www.hwaci.com/sw/sqlite/quickstart.html tutorial}._
+ 
  *:  Spawn two or more threads. Each one, opens the db via *sqlite_open()* and keeps its own copy of sqlite structure.
  
  *:  Each thread then proceeds to insert a number of records, let's say 1000. The problem you will encounter is the following: one thread will get control over the database by setting a lock on the file. This is fine, but the rest of the threads will keep on failing for each attempted *INSERT* _while_ the lock is active.
  
  **Solution**
  
! Test for {link: http://www.hwaci.com/sw/sqlite/faq.html#q7 SQLITE_BUSY}, which I didn't do originally. Here's some pseudo-code to illustrate a solution:
  
    while (continueTrying) {
      retval = sqlite_exec(db, sqlQuery, callback, 0, &msg);