Please enable JavaScript.
Coggle requires JavaScript to display documents.
Concurency (Operations (Knowledge (For building functionality that should…
Concurency
Operations
Knowledge
-
-
-
-
IsReady, isExecuting, isCancelled, isFinished
-
-
-
-
OperationQueues
-
AsyncOperation
-
Want to make an operation async, need to make subclass of the Async Operation class
-
-
Concurrency Problems
-
-
Race Conditions
-
How to solve:
-
private let threadSafeCountQueue = DispatchQueue(label: "...", attributes: .concurrent)
private var _count = 0
public var count: Int {
get {
return threadSafeCountQueue.sync {
return _count
}
}
set {
threadSafeCountQueue.async(flags: .barrier) { [unowned self] in
self._count = newValue
}
}
}
Grand Center Dispatch
Knowledge
-
-
-
Serial
Single thread, single task at any given time
Concurrent
Multi thread, multi tasks
Details
-
DispatchQueue with label, default is serial, can be defined as concurrent
-
-
Semaphores
Knowledge
-
-
-
use with queue and group
let semaphore = DispatchSemaphore(value: 4)
for i in 1...10 {
queue.async(group: group) {
semaphore.wait()
defer { semaphore.signal() }
}
}
-