2.7 · NSCache and locks
Two tools for sharing memory safely and cheaply: a cache the system is allowed to empty, and the locks that stop two threads corrupting the same data. Neither does what its name suggests until you know its limits.
Mental model. A cache is a promise you can be wrong about (“it might not be here”). A lock is a promise you must never break (“only one of us at a time”).
Caching
What NSCache guarantees, and what a real cache needs on top.
- What is NSCache, and how is it different from a Dictionary?
NSCache is an in-memory key-value store built for disposable data. It is thread-safe, it does not copy its keys, and the system may evict items at any time… - How would you design an in-memory image cache?
Start from the access pattern. Cache decoded images at the size you display them (decoding is the expensive part), keyed by URL plus size, with a cost equal to…
Locks and shared state
Choosing a primitive, using it with async code, and making state safe.
- NSLock, NSRecursiveLock, os_unfair_lock, Mutex, serial queue: how do you choose?
All of them give mutual exclusion: one thread in the critical section at a time. They differ in re-entrancy, cost and API. NSLock is the simple default, but it… - Why can’t you hold a lock across an await, and what does the compiler say?
A lock is owned by a thread. A task that suspends at an await may resume on a different thread, so unlocking afterwards can be the wrong thread, and in the… - Is Array thread-safe, and how do you make shared state safe?
No. Several threads may read an array at the same moment, but any write that overlaps another read or write is a data race: undefined behaviour, in practice a…
More in Foundation & stdlib
- 2.1 · Collections, indexing, Sequence and lazy
- 2.2 · Date, Calendar, Locale, TimeZone, FormatStyle
- 2.3 · URL, URLComponents, Data, FileManager
- 2.4 · NotificationCenter, KVO and Operation
- 2.5 · JSON coder options, property lists, UserDefaults
- 2.6 · Bundle, resources, localization tables
- 2.8 · Attributed strings, scanners, number formatters
- 2.9 · Bridging to Objective-C Foundation types