Skip to content

Instantly share code, notes, and snippets.

@st-small
Created July 22, 2019 17:50
Show Gist options
  • Save st-small/56e809eb6e9ccac427f2d7f8321b3662 to your computer and use it in GitHub Desktop.
Save st-small/56e809eb6e9ccac427f2d7f8321b3662 to your computer and use it in GitHub Desktop.
let lock = NSLock()
var counter: Int = 0 {
didSet{
print("Counter value is \(counter)")
}
}
let thrd1 = Thread {
for _ in 0..<1000 {
lock.lock()
let temp = counter + 1
counter = temp
lock.unlock()
}
}
let thrd2 = Thread {
for _ in 0..<1000 {
lock.lock()
let temp = counter + 1
counter = temp
lock.unlock()
}
}
thrd1.start()
thrd2.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment