Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save migueldeicaza/7b383f9481e95fab8b5d16943eccf545 to your computer and use it in GitHub Desktop.
Save migueldeicaza/7b383f9481e95fab8b5d16943eccf545 to your computer and use it in GitHub Desktop.
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