From the course: Practical Design Patterns in Swift

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Readers-writer lock

Readers-writer lock - Swift Tutorial

From the course: Practical Design Patterns in Swift

Start my 1-month free trial

Readers-writer lock

- [Instructor] Let me show you a technique that's known as the reader's right lock. You can find the project in the exercise files folder. Chapter two, two five, begin. Multiple threads can retrieve values from the AppSettings Singleton simultaneously without causing any concurrency hazards. Problems occur when multiple threads are modifying the Singleton's internal state at once. Also, writing and reading the internal storage shouldn't happen in parallel. Thus, we could allow concurrent read operations which would bring performance improvements. There is no need to serialize the execution of the string for key and int for key methods. However, we should prevent reading or writing the settings dictionary while a set value for key method is updating its contents. The update needs to complete before allowing other threads to access the dictionary. Grand central dispatch solves this problem using dispatch barriers. The barrier changes the concurrent queue into a serial queue for as long…

Contents