Audit threading issues in your code.
- Xcode 8.0+
The Thread Sanitizer, or TSan, is an LLVM based tool for Swift and C languages that detects data races at runtime. Data races occur when multiple threads access the same memory without synchronization and at least one access is a write. Data races are dangerous because they can cause programs to behave unpredictably, or even result in memory corruption.
TSan also detects other threading bugs, including uninitialized mutexes and thread leaks.
How TSan Works
The Thread Sanitizer records the information about each memory access, and checks whether that access participates in a race. All memory accesses in the code is transformed by the compiler in the following way:
Each thread stores its own timestamp and the timestamps for other threads in order to establish points of synchronization. Timestamps are incremented each time memory is accessed. By checking for consistency of memory access across threads, data races can be detected independent of the actual timing of the access. Therefore, the Thread Sanitizer can detect races even if they didn't manifest during a particular run.
Running your code with Thread Sanitizer checks enabled can result in CPU slowdown of 2⨉ to 20⨉, and an increase in memory usage by 5⨉ to 10⨉. You can improve memory utilization and CPU overhead by compiling at the -O1 optimization level.