SQLite Forum

Multi-threading sluggishess on Mac?
Login
I had cases where a small-transaction executed from multi-threads took relatively long time.<br>
This happens to me only on Mac.<br>
Same code on Windows is working very fast.<br>

Environment:

* sqlite version: 3.28.0
* OS: Mac High Sierra 10.13.6
* IDE: xcode Version 10.0 (10A255)
* SQLite configuration:
 * Multi-threading mode (SQLITE_THREADSAFE==2).
 * One connection per-thread.
 * WAL-journal with synchronous set to NORMAL.

I noticed that while performing transaction from multiple-threads, sqlite enters sleep from the following stack:

* `sleep ()`
* `unixSleep ()`
* `sqlite3OsSleep ()`
* `walTryBeginRead ()`
* `sqlite3WalBeginReadTransaction ()`
* `pagerBeginReadTransaction ()`
* `sqlite3PagerSharedLock ()`
* `lockBtree ()`
* `sqlite3BtreeBeginTrans ()`
* `sqlite3VdbeExec ()`
* `sqlite3Step ()`
* `sqlite3_step ()`
* `sqlite3_exec ("BEGIN IMMEDIATE TRANSACTION;")`


What I noticed is that in these cases, and while halting the debugger, I could have few threads (3~5) in sleep while **no other thread is active in sqlite code**.<br>
This is possible because the sleep is not an efficient-sleep, but rather full-sleep for hard-coded period of time (several micro seconds?).<br>
This can cause a situation where after the blocking-thread **did release database-lock**, the other threads are not working yet as they are still asleep.

Sleep location in sqlite code:

    static int walTryBeginRead(...){
	
        ...other code...
		
        if( cnt>5 ){
            int nDelay = 1;                      /* Pause time in microseconds */
            if( cnt>100 ){
                VVA_ONLY( pWal->lockError = 1; )
                return SQLITE_PROTOCOL;
            }

            if( cnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39;
            sqlite3OsSleep(pWal->pVfs, nDelay);	 <--- THE SLEEP
	}


My question:<br>
Can I modify something in sqlite build flags in order to make these sleeps less often?

Thanks, PazO