Skip to content

Instantly share code, notes, and snippets.

@sharplet
Created June 18, 2019 14:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sharplet/fa6d118c3f2ae30eeea6dfc0815b0009 to your computer and use it in GitHub Desktop.
Save sharplet/fa6d118c3f2ae30eeea6dfc0815b0009 to your computer and use it in GitHub Desktop.
Demo of DispatchSourceSignal for handling interrupts
import Darwin.C
import Dispatch
signal(SIGINT, SIG_IGN)
let source = DispatchSource.makeSignalSource(signal: SIGINT, queue: .main)
var count = 0
source.setEventHandler {
count += 1
print("Interrupted \(count) times.")
if count == 3 {
exit(0)
}
}
source.resume()
dispatchMain()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment