Member-only story

Concurrent Programming

Guard Access to Your Resources

Designing a Thread-Safe API

Riccardo Cipolleschi
6 min readJun 4, 2020

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

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Riccardo Cipolleschi
Riccardo Cipolleschi

Written by Riccardo Cipolleschi

Hey there, I’m Riccardo. Software engineer at Meta. I have a passion for iOS and I love to share my knowledge with others.

No responses yet

Write a response