Skip to content

Instantly share code, notes, and snippets.

@seanlilmateus
Last active November 22, 2017 14:02
Show Gist options
  • Save seanlilmateus/d98aeeb1920fe4b93e6a to your computer and use it in GitHub Desktop.
Save seanlilmateus/d98aeeb1920fe4b93e6a to your computer and use it in GitHub Desktop.
swift Go like channels
import Foundation
// Playground - noun: a place where people can play
class Channel<T> {
var stream: Array<T>
let queue: dispatch_queue_t
let semaphore: dispatch_semaphore_t
init() {
self.stream = []
self.semaphore = dispatch_semaphore_create(0)
self.queue = dispatch_queue_create("channel.queue.", DISPATCH_QUEUE_CONCURRENT)
}
func write(value: T) {
dispatch_async(self.queue) {
self.stream += value
dispatch_semaphore_signal(self.semaphore)
}
}
func recv() -> T {
var result:T?
dispatch_sync(self.queue) {
dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER)
result = self.stream.removeAtIndex(0)
}
return result!
}
}
let chan: Channel<Int> = Channel()
let delay = 1.5 * Double(NSEC_PER_SEC)
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(time, dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
chan.write(20)
})
chan.write(1)
chan.write(5)
chan.write(2)
let x1 = chan.recv()
let x2 = chan.recv()
println((x1, x2)) // 1, 2
let name = "result \(x1)" // "result 1"
let name2 = "result \(x2)" // "result 5"
let name3 = "result \(chan.recv())" // "result 2"
let name4 = "result \(chan.recv())" // "result 20" after 1.5 secs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment