Skip to content

Instantly share code, notes, and snippets.

@zntfdr
Last active March 21, 2017 19:23
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save zntfdr/3d926ceee1b5a861c211e3a268e42c19 to your computer and use it in GitHub Desktop.
import Foundation
import PlaygroundSupport
let higherPriority = DispatchQueue.global(qos: .userInitiated)
let lowerPriority = DispatchQueue.global(qos: .utility)
let semaphore = DispatchSemaphore(value: 1)
func asyncPrint(queue: DispatchQueue, symbol: String) {
queue.async {
print("\(symbol) waiting")
semaphore.wait() // requesting the resource
for i in 0...10 {
print(symbol, i)
}
print("\(symbol) signal")
semaphore.signal() // releasing the resource
}
}
asyncPrint(queue: higherPriority, symbol: "🔴")
asyncPrint(queue: lowerPriority, symbol: "🔵")
PlaygroundPage.current.needsIndefiniteExecution = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment