Skip to content

Instantly share code, notes, and snippets.

@toshi0383
Created October 1, 2019 08:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toshi0383/41f6fba0576b0bcea89e70776f56101b to your computer and use it in GitHub Desktop.
Save toshi0383/41f6fba0576b0bcea89e70776f56101b to your computer and use it in GitHub Desktop.
import Foundation
let smp = DispatchSemaphore(value: 1)
let smp1 = DispatchSemaphore(value: 0)
func doSomething() {
for i in (0..<4) {
smp.wait()
print("[\(i * 3)]")
defer {
smp.signal()
}
if i % 3 == 2 {
return // continue
}
DispatchQueue.global().asyncAfter(deadline: .now() + .milliseconds(50)) {
print("[\(i * 3 + 1)] ok")
smp1.signal()
}
smp1.wait()
print("[\(i * 3 + 2)] after wait")
}
}
doSomething()
RunLoop.main.run(until: Date(timeIntervalSinceNow: 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment