Avatar
norio_nomura 3/9/2018 2:43 PM
この結果を見ると、isKnownUniquelyReferenced()って排他制御が無いとあてにならない? import Foundation struct S { class C { var name: String init(name: String) { self.name = name print("init: \(name)") } deinit { print("deinit: \(name)") } } var c: C init(name: String) { c = C(name: name) } var name: String { get { return c.name } set { if isKnownUniquelyReferenced(&c) { print("unique: \(c.name) -> \(newValue)") c.name = newValue } else { print("not unique: \(c.name) -> \(newValue)") c = C(name: newValue) } } } } var s = S(name: "初期値") let queue = DispatchQueue(label: "keep unique") DispatchQueue.concurrentPerform(iterations: 10) { idx in // queue.sync { s.name = "\(idx)" // } } print(s.name) init: 初期値 unique: 初期値 -> 0 unique: 初期値 -> 1 unique: 初期値 -> 2 unique: 初期値 -> 3 unique: 1 -> 4 unique: 1 -> 5 not unique: 5 -> 6 init: 6 deinit: 5 unique: 6 -> 7 not unique: 6 -> 8 init: 8 not unique: 7 -> 9 init: 9 deinit: 7 deinit: 8 9 (edited)