Avatar
Thread safe な dictionary を作ろうとしているのですが,dead lock になってしまうようです.何がまずいかわかりますか? public class SynchronizedDictionary<Key: Hashable, Value> { private var dictionary: [Key: Value] = [:] private let queue = DispatchQueue(label: "hoge", qos: .default, attributes: .concurrent) public subscript(_ key: Key) -> Value? { get { queue.sync { dictionary[key] } } set { queue.async(flags: .barrier) { self.dictionary[key] = newValue } } } } var a = SynchronizedDictionary<Int, Int>() let n = 100000 DispatchQueue.concurrentPerform(iterations: n) { i in a[i] = i } print(a[n-1]) write のところは sync ではなく async(flags: .barrier) とした方が良いという記事をいくつか見てそうしてます.