Created
June 18, 2019 14:14
-
-
Save sharplet/fa6d118c3f2ae30eeea6dfc0815b0009 to your computer and use it in GitHub Desktop.
Demo of DispatchSourceSignal for handling interrupts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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