Created
October 1, 2019 08:11
-
-
Save toshi0383/41f6fba0576b0bcea89e70776f56101b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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