From 5781cdd0cd43250664bc8faceed8a4834064aa0b Mon Sep 17 00:00:00 2001 From: Vladimir Kovalev <115025494+vladimirkoff@users.noreply.github.com> Date: Wed, 18 Jun 2025 17:46:17 +0300 Subject: [PATCH] Create memory-swift --- memory-swift | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 memory-swift diff --git a/memory-swift b/memory-swift new file mode 100644 index 0000000..caef643 --- /dev/null +++ b/memory-swift @@ -0,0 +1,38 @@ +import Foundation + +final class SharedPoint { + private var x: Int32 + private var y: Int32 + private let lock = DispatchQueue(label: "sharedpoint-queue") + + init(x: Int32, y: Int32) { + self.x = x + self.y = y + } + + func move(dx: Int32, dy: Int32) { + lock.sync { + x &+= dx + y &+= dy + } + } + + func clone() -> SharedPoint { + return lock.sync { + return SharedPoint(x: x, y: y) + } + } + + func toString() -> String { + return lock.sync { + return "(\(x), \(y))" + } + } +} + +let p1 = SharedPoint(x: 10, y: 20) +print(p1.toString()) // (10, 20) + +let c1 = p1.clone() +c1.move(dx: -5, dy: 10) +print(c1.toString()) // (5, 30)