Skip to content

Instantly share code, notes, and snippets.

@glessard
Last active August 31, 2016 05:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glessard/826241431dcea3655d1e to your computer and use it in GitHub Desktop.
Save glessard/826241431dcea3655d1e to your computer and use it in GitHub Desktop.
print, synchronized for multiple threads
//
// syncprint.swift
//
// Created by Guillaume Lessard on 2014-08-22.
// Copyright (c) 2016 Guillaume Lessard. All rights reserved.
//
// https://github.com/glessard/syncprint
// https://gist.github.com/glessard/826241431dcea3655d1e
//
import Dispatch
import Foundation.NSThread
private let PrintQueue = DispatchQueue(label: "com.tffenterprises.syncprint")
private let PrintGroup = DispatchGroup()
private var silenceOutput: Int32 = 0
/// A wrapper for `Swift.print()` that executes all requests on a serial queue.
/// Useful for logging from multiple threads.
///
/// Writes a basic thread identifier (main or back), the textual representation
/// of `item`, and a newline character onto the standard output.
///
/// The textual representation is from the `String` initializer, `String(item)`
///
/// - parameter item: the item to be printed
public func syncprint(_ item: Any)
{
let thread = Thread.current.isMainThread ? "[main]" : "[back]"
PrintQueue.async(group: PrintGroup) {
// Read silenceOutput atomically
if OSAtomicAdd32(0, &silenceOutput) == 0
{
print(thread, item, separator: " ")
}
}
}
/// Block until all tasks created by syncprint() have completed.
public func syncprintwait()
{
// Wait at most 200ms for the last messages to print out.
let res = PrintGroup.wait(timeout: DispatchTime.now() + 0.2)
if res == .timedOut
{
OSAtomicIncrement32Barrier(&silenceOutput)
PrintGroup.notify(queue: PrintQueue) {
print("Skipped output")
OSAtomicDecrement32Barrier(&silenceOutput)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment