iOS Concurrency with GCD and Operations, Episode 18: Make Class Threadsafe | Kodeco

Learn how to use Xcode's TSan tool to detect data races, then use a dispatch barrier to make a class threadsafe.


This is a companion discussion topic for the original entry at https://www.kodeco.com/9461083-ios-concurrency-with-gcd-and-operations/lessons/18

I tried the concurrency project in XCode 14.3.1. TSAN doesnโ€™t display a purple warning but shows the following error message in the XCode console instead.
Concurrency(83742,0x101184240) malloc: nano zone abandoned due to inability to reserve vm space.
Of course, if I remove the thread sanitizer option, the message will disappear.

hi Suppasit! The Concurrency project doesnโ€™t trigger TSan in Xcode 14. NameChanger does โ€” try that? Or, Iโ€™ve rewritten that project for the course update Iโ€™m currently working on โ€” I sleep a random time in both for loops:

func raceYou() {
  var counter = 0

  let queue = DispatchQueue(label: "notMain")
  queue.async {
    for _ in 1 ... 10 {
      Thread.sleep(forTimeInterval: Double.random(in: 0.1..<0.5))
      counter += 1
    }
    print(">>> notMain Queue counter = \(counter)")
  }

  DispatchQueue.main.async {
    for _ in 1 ... 10 {
      Thread.sleep(forTimeInterval: Double.random(in: 0.1..<0.5))
      counter += 1
    }
    print(">>> Main Queue counter = \(counter)")
  }
}
1 Like

Hello Audrey,

Thank you for your comment. I have tried the code you provided, and TSAN is reporting an error on the main queue. Could this possibly mean that the main queue is causing a race condition by accessing the same variable as the custom queue?

hi Suppasit, the error can appear in either queue. Both are changing the value of counter.

1 Like

Hi Audrey, Thank you for the answer. :slightly_smiling_face:

1 Like