我正在使用Grand Central Dispatch将一个数组的元素转换为另一个数组.我调用dispatch_apply源数组,将其转换为零个或多个项,然后将它们添加到目标数组.这是一个简化的例子:
let src = Array(0..<1000)
var dst = [UInt32]()
let queue = dispatch_queue_create("myqueue", DISPATCH_QUEUE_CONCURRENT)
dispatch_apply(src.count, queue) { i in
dst.append(arc4random_uniform(UInt32(i))) // <-- potential error here
}
print(dst)
Run Code Online (Sandbox Code Playgroud)
我有时会append在线上出错.错误总是以下之一:
1. malloc: *** error for object 0x107508f00: pointer being freed was not allocated
2. fatal error: UnsafeMutablePointer.destroy with negative count
3. fatal error: Can't form Range with end < start
Run Code Online (Sandbox Code Playgroud)
我想这是由于append不是线程安全的.我做错了什么以及如何解决?