Avatar
@swift-5.6.3 import Foundation let workCount = 1000 class Counter { let lock = NSLock() var count = 0 func increment() { self.lock.lock() defer { self.lock.unlock() } self.count += 1 } } let counter = Counter() for _ in 0..<workCount { Task { counter.increment() } } Thread.sleep(forTimeInterval: 2) print("counter.count", counter.count) (edited)