How to resolve the algorithm Atomic updates step by step in the Swift programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Atomic updates step by step in the Swift programming language
Table of Contents
Problem Statement
Define a data type consisting of a fixed number of 'buckets', each containing a nonnegative integer value, which supports operations to: In order to exercise this data type, create one set of buckets, and start three concurrent tasks:
The display task need not be explicit; use of e.g. a debugger or trace tool is acceptable provided it is simple to set up to provide the display. This task is intended as an exercise in atomic operations. The sum of the bucket values must be preserved even if the two tasks attempt to perform transfers simultaneously, and a straightforward solution is to ensure that at any time, only one transfer is actually occurring — that the transfer operation is atomic.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Atomic updates step by step in the Swift programming language
Source code in the swift programming language
import Foundation
final class AtomicBuckets: CustomStringConvertible {
var count: Int {
return buckets.count
}
var description: String {
return withBucketsLocked { "\(buckets)" }
}
var total: Int {
return withBucketsLocked { buckets.reduce(0, +) }
}
private let lock = DispatchSemaphore(value: 1)
private var buckets: [Int]
subscript(n: Int) -> Int {
return withBucketsLocked { buckets[n] }
}
init(with buckets: [Int]) {
self.buckets = buckets
}
func transfer(amount: Int, from: Int, to: Int) {
withBucketsLocked {
let transferAmount = buckets[from] >= amount ? amount : buckets[from]
buckets[from] -= transferAmount
buckets[to] += transferAmount
}
}
private func withBucketsLocked<T>(do: () -> T) -> T {
let ret: T
lock.wait()
ret = `do`()
lock.signal()
return ret
}
}
let bucks = AtomicBuckets(with: [21, 39, 40, 20])
let order = DispatchSource.makeTimerSource()
let chaos = DispatchSource.makeTimerSource()
let printer = DispatchSource.makeTimerSource()
printer.setEventHandler {
print("\(bucks) = \(bucks.total)")
}
printer.schedule(deadline: .now(), repeating: .seconds(1))
printer.activate()
order.setEventHandler {
let (b1, b2) = (Int.random(in: 0..<bucks.count), Int.random(in: 0..<bucks.count))
let (v1, v2) = (bucks[b1], bucks[b2])
guard v1 != v2 else {
return
}
if v1 > v2 {
bucks.transfer(amount: (v1 - v2) / 2, from: b1, to: b2)
} else {
bucks.transfer(amount: (v2 - v1) / 2, from: b2, to: b1)
}
}
order.schedule(deadline: .now(), repeating: .milliseconds(5))
order.activate()
chaos.setEventHandler {
let (b1, b2) = (Int.random(in: 0..<bucks.count), Int.random(in: 0..<bucks.count))
bucks.transfer(amount: Int.random(in: 0..<(bucks[b1] + 1)), from: b1, to: b2)
}
chaos.schedule(deadline: .now(), repeating: .milliseconds(5))
chaos.activate()
dispatchMain()
You may also check:How to resolve the algorithm Odd word problem step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm File extension is in extensions list step by step in the Julia programming language
You may also check:How to resolve the algorithm Sort stability step by step in the Wren programming language
You may also check:How to resolve the algorithm Runtime evaluation step by step in the Julia programming language
You may also check:How to resolve the algorithm Merge and aggregate datasets step by step in the Raku programming language