This repo is a personal project and doesn't contain anything intended for use in a production setting.
This is a Swift 5.0 playground that serves as a scratchpad for implementing a minimal equivalent of DispatchQueue
using only the condition classes provided by Foundation. Only NSThread
, NSCondition
and NSConditionLock
are used which provides enough functionality to suspend and resume threads only when needed.
The implementation is called SimpleQueue
and is modelled after DispatchQueue
. Since this is a minimal demo project, most of the functionality DispatchQueue
offers beyond async()
/ sync()
isn't provided. The equivalent of a concurrent queue is configured by configuring SimpleQueue
with a workerCount
greater than 1.
The "unchecked rethrows
" trick used by DispatchQueue
to circumvent the compiler isn't used here, since it apparently exploits a compiler bug. withoutActuallyEscaping()
is used to to implement SimpleQueue.sync()
.
final class SimpleQueue {
init(workerCount: Int)
func async(_ work: @escaping () -> Void)
@discardableResult
func sync<Result>(_ work: () -> Result) -> Result
}
- A better set tests
Xcode 12.5.1 was used to create the playground.