Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Alternative to semaphore locking around a task
init () {
//let _ = LinuxTop (source: try! String (contentsOfFile: "/Users/miguel/cvs/CpuStatus/pi-top-output"))
//let _ = MacTop (source: try! String (contentsOfFile: "/Users/miguel/cvs/CpuStatus/mac-top-output"))
Task {
print ("await/Starting")
let x: Bool = await withCheckedContinuation { cc in
Task {
print ("await/Sleeping")
try? await Task.sleep(nanoseconds: 2_000_000_000)
print ("await/signal")
cc.resume(returning: true)
}
}
print ("await/wait")
//await x.wait ()
print ("await/done")
}
// Task {
// let x = DispatchSemaphore(value: 0)
// Task.detached {
// await try? Task.sleep(nanoseconds: 2_000_000_000)
// x.signal ()
// print ("signaled")
// }
// x.wait()
// }
print ("waited")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment