Audit threading issues in your code.
SDK
- Xcode 8.0+
Overview
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.
Important
TSan is supported only for 64-bit macOS and 64-bit iOS and tvOS simulators (watchOS is not supported). You cannot use TSan when running apps on a device.
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:
Pseudocode for Thread Sanitizer memory access
// Before
*address = ...; // or: ... = *address;
// After
RecordAndCheckWrite(address);
*address = ...; // or: ... = *address;
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.
Performance Impact
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.