SQLite Forum

Mutex with Sqlite

Mutex with Sqlite

(1) By SourceCode (sourcecode11) on 2020-11-25 17:23:35 [link] [source]

Can someone please give an example of how to use mutex with an example?

Trying to understand the use of sqlite3_mutex_enter or sqlite3_mutex_try when it comes to serializing access to the database to avoid race conditions for read/write operations.

(2) By Keith Medcalf (kmedcalf) on 2020-11-25 19:38:14 in reply to 1 [link] [source]

You don't do that.

Or rather, you should perhaps explain WHY you think you need to do that and why you think the builtin protection is insufficient and what it is, exactly, that you are trying to achieve.

In other words, what exactly and precisely is the problem you are seeing for which you think this could be a solution, and why do you think that?

Putting the solution before the problem is almost always the wrong way to look for a solution. Preconceptions and assumptions might not be correct.

(3) By Ryan Smith (cuz) on 2020-11-25 19:58:25 in reply to 1 [source]

To add some context to Keith's very correct reply:

SQLite uses these internally and already does a pretty amazing job of controlling multi-threaded access if you let it.

You are allowed to create mutexes, gain access to those used by SQLite or use them for your own purposes, so these calls are exposed, but they are not required for you to call them for SQLite to operate normally.

If you are asking out of interest of how sqlite itself uses them to control multi-threaded access - Nobody typically does this, so I suppose the best examples are where they are used in the SQLite code itself. (Which you can download and browse freely).

Hope that helps!