This is where we will add some finesse with a common threading approach known as asynchronous locking. Since there will only be one instance of the SQLiteStorage object, this means we have the possibility of a race condition as multiple threads can make changes to the same database connection at the same time.
Locking is the most common C# approach for restricting shared resources between multiple threads. In order to avoid this situation, we create an object for locking as follows:
private Object lockObject = new Object();
Then, to restrict code blocks to one thread at any one time, we do the following:
lock (thisLock) { ... }
This is the perfect approach when our code is synchronous. The problem we have is our SQLite implementation is asynchronous, and the restriction with basic locking is we cannot execute asynchronous code inside a lock statement. This is where we have to implement the async-lock pattern.
3.138.200.66