4

我偶然发现了 Swift 5 中的并发和数组问题。为了重现该问题,我将代码简化为以下片段:

import Dispatch

let group = DispatchGroup()
let queue = DispatchQueue(
  label: "Concurrent threads",
  qos: .userInitiated,
  attributes: .concurrent
)

let threadCount = 4
let size = 1_000
var pixels = [SIMD3<Float>](
  repeating: .init(repeating: 0),
  count: threadCount*size
)

for thread in 0..<threadCount {
  queue.async(group: group) {
    for number in thread*size ..< (thread+1)*size {
      let floating = Float(number)
      pixels[number] = SIMD3<Float>(floating, floating, floating)
    }
  }
}

print("waiting")
group.wait()
print("Finished")

当我使用 Xcode 版本 10.2 beta 4 (10P107d) 在调试模式下执行此操作时,它总是崩溃并出现如下错误:

Multithread(15095,0x700008d63000) malloc: *** error for object 0x104812200: pointer being freed was not allocated
Multithread(15095,0x700008d63000) malloc: *** set a breakpoint in malloc_error_break to debug

我觉得这是编译器中的一些错误,因为当我在发布模式下运行代码时,它运行得很好。还是我在这里做错了什么?

4

1 回答 1

9

数组中的指针绝对可以在你的脚下改变。它不是原始内存。

数组不是线程安全的。数组是值类型,这意味着它们以线程安全的方式支持写入时复制(因此您可以自由地将数组传递给另一个线程,如果它被复制到那里,那没关系),但是您不能在多个线程上改变相同的数组。数组不是 C 缓冲区。它没有承诺具有连续的内存。甚至根本没有承诺分配内存。Array 可以在内部选择将“我目前全为零”存储为特殊状态,并为每个下标返回 0。(它没有,但它被允许。)

对于这个特定问题,您通常会使用 vDSP_vramp 之类的 vDSP 方法,但我知道这只是一个示例,可能没有解决问题的 vDSP 方法。不过,通常情况下,我仍会专注于 Accelerate/SIMD 方法,而不是分派到队列。

但是如果你要分派到队列,你需要一个 UnsafeMutableBuffer 来控制内存(并确保内存甚至存在):

pixels.withUnsafeMutableBufferPointer { pixelsPtr in
    DispatchQueue.concurrentPerform(iterations: threadCount) { thread in
        for number in thread*size ..< (thread+1)*size {
            let floating = Float(number)
            pixelsPtr[number] = SIMD3(floating, floating, floating)
        }
    }
}

“不安全”表示现在您的问题是确保所有访问都是合法的并且您没有创建竞争条件。

注意.concurrentPerform这里的使用。正如@user3441734 提醒我们的那样,pixelsPtr不承诺一旦.withUnsafeMutablePointer完成就有效。.concurrentPerform保证在所有块都完成之前不会返回,因此保证指针有效。

这也可以使用 DispatchGroup 完成,但.wait需要在withUnsafeMutableBufferPointer.

于 2019-03-16T13:20:44.067 回答