Skip to content

Instantly share code, notes, and snippets.

@zntfdr zntfdr/semaphoreExample2.swift Secret
Last active Mar 21, 2017

What would you like to do?
import Foundation
import PlaygroundSupport
let higherPriority = .userInitiated)
let lowerPriority = .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
You can’t perform that action at this time.