// 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) } } }