How to avoid race condition when accessing properties in a singleton using GCD

Spencer Feng
1 min readNov 4, 2021

Thread safety is very important and it is difficult to find problems caused by violating thread safety rules. In this article, I will talk about how to make getting and setting properties in a singleton in thread-safe manner.

Given we have a single class like the one below which has a property that can be read and written, because the read and write access can happen in different threads, it may cause a race condition.

--

--