Avatar
上のライブラリを使って,こういう感じに実装することにしました: let semaphore = DispatchSemaphore(value: 0) var exceedMemory = false DispatchQueue.global(qos: .userInitiated).async { main() semaphore.signal() } DispatchQueue.global(qos: .background).async { while true { let mem = getCurrentRSS() if mem > self.config.maxMemory { exceedMemory = true semaphore.signal() } sleep(10) } } if semaphore.wait(timeout: .now() + timeout) == .timedOut { throw AppError.timeout(sec: config.timeout) } if exceedMemory { throw AppError.exceedMemory(limit: config.maxMemoryGB) }