Skip to content

Instantly share code, notes, and snippets.

@onmyway133
Created February 3, 2016 06:53
Show Gist options
  • Save onmyway133/3ad9c8c566465667762f to your computer and use it in GitHub Desktop.
Save onmyway133/3ad9c8c566465667762f to your computer and use it in GitHub Desktop.
PingThread.swift
// from https://dl.dropboxusercontent.com/u/3820193/VbierClientUnderTheHood.pdf
class PingThread : NSThread {
var pingTaskIsRunning = false
var semaphore = dispatch_semaphore_create(0)
override func main() {
while !self.cancelled {
pingTaskIsRunning = true
dispatch_async(dispatch_get_main_queue()) {
self.pingTaskIsRunning = false
dispatch_semaphore_signal(self.semaphore)
}
NSThread.sleepForTimeInterval(0.4)
if pingTaskIsRunning {
// warning here
}
dispatch_semaphore_wait(semaphore,
DISPATCH_TIME_FOREVER)
}
}
}
@balrajOla
Copy link

Is there a swift4 version of it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment