CFAttributedString 线程安全

CFAttributedString thread safety

这段代码是否表明 CFAttributedString 不是线程安全的?还是我设置有误?

我认为从多线程读取 CFAttributedString 是安全的,但我发现这段代码每隔几次运行就会崩溃。

@IBAction func testIt(_ sender: Any?) {
    let testString = "Hello world! Lets make this a bit longerrrrrrrrrrrrrrrr." as CFString
    let testStringLength = CFStringGetLength(testString)

    let testAttributedString = CFAttributedStringCreateMutable(kCFAllocatorDefault, testStringLength)
    CFAttributedStringReplaceString(testAttributedString, CFRange(location: 0, length: 0), testString)
    CFAttributedStringEndEditing(testAttributedString)
    for i in 0..<testStringLength {
        let range = CFRange(location: i, length: 1)
        let keyAndValue = "\(i)" as CFString
        CFAttributedStringSetAttribute(testAttributedString, range, keyAndValue, keyAndValue)
    }

    let immutableTestAttributedString = CFAttributedStringCreateCopy(kCFAllocatorDefault, testAttributedString)
    DispatchQueue.concurrentPerform(iterations: 100) { _ in
        var index: CFIndex = 0
        var effectiveRange: CFRange = CFRange(location: 0, length: 0)
        while index < testStringLength {
            // Crash happens here EXC_BAD_ACCESS (code=1, address=0x24)
            let _ = CFAttributedStringGetAttributes(immutableTestAttributedString, index, &effectiveRange)
            index = effectiveRange.location + effectiveRange.length
        }
    }
}
let testString = "Hello world! Lets make this a bit longerrrrrrrrrrrrrrrr." as CFString

这是一个伪装成 CFString 的 Swift 字符串,它大约有两层间接寻址,并在内部调用一个非常不同的代码路径(是否仍然有效是你和 radar).

尝试创建一个合适的 CFString 并查看它是否按您预期的方式工作。

var bytes = Array("Hello world! Lets make this a bit longerrrrrrrrrrrrrrrr.".utf16)
let testString = CFStringCreateWithCharacters(nil, &bytes, bytes.count)

(当然,我强烈建议在 NSAttributedString 而不是 CFAttributedString 中完成所有这些工作。与 Swift->Foundation 桥接相比 Swift->Foundation 桥接要简单得多并且经常使用=29=]->Foundation->CoreFoundation 桥接。这可能只是那个桥接中的一个错误,但如果你避免它,你的世界仍然会容易得多。)


虽然我无法使用纯 CFString 重现问题,但这绝对不是线程安全的。 CoreFoundation 是开源的(有点……,但足以满足此目的),因此您可以自己查看代码。最终 CFAttributedStringGetAttributes 调用 blockForLocation,它会更新内部缓存并且不会对其进行锁定。我没有在文档中看到任何承诺这是线程安全的内容。