Member-only story
Concurrent Programming
Guard Access to Your Resources
Designing a Thread-Safe API

Nowadays, apps strive to start fast. They give users the impression of being fast and reliable. We, as developers, achieve this by writing concurrent, parallel code. The obtained performances are amazing when we manage to write it correctly. However, writing a proper concurrent code is anything but a simple matter. There are several aspects that, if forgotten, can have serious consequences: the most common ones are run-time crashes with some of the unclearest messages ever obtained.
I’d like to show these concepts using an example: let’s imagine that we are writing an InMemoryDatabase
. It is a piece of software that keeps your models in memory so that you can read and updates them easily.
Basic API
In their most basic form, the API of this manager is pretty simple:
- there should be a way to get an entry from the database.
- there should be a way to update an entry in the database.
The initial code will look like this