Adding additional threading techniques

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.

Tip

Race conditions are a common threading issue where multiple threads try to perform operations at the same time on shared data.

How do we solve this problem?

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.200.66