Group Group Group Group Group Group Group Group Group

Race Conditions example

In the first example to prevent race conditions

public var count: Int {
set {
threadSafeCountQueue.sync {
_count = newValue
}
}
}

I wonder if it also could be

public var count: Int {
set {
threadSafeCountQueue.async {
_count = newValue
}
}
}

Hi, @linyuta. I wouldn’t do that. If you’re performing a write operation you want to be sure that no more of your code runs until the write completes. Imagine if you’re in a tight light incrementing the value. You wouldn’t want to hit the next line of code, which might read the value of the variable, until the write has actually completed.

1 Like