我在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
我感觉这是编译器的一个 bug,因为当我以发布模式运行代码时,它可以正常运行。或者是我的做法有误?