并行添加到数组

Jen*_*nny 5 grand-central-dispatch swift swift2

我正在使用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不是线程安全的.我做错了什么以及如何解决?

Cod*_*ent 3

正如您所发现的,Swift 可能会重新定位数组以提高内存效率。当你运行多个时append,它必然会发生。我的猜测是,与转换相比,附加到数组是一个成本非常低的操作。您可以创建一个串行队列来扩展dst数组:

let src = Array(0..<1000)
var dst = [UInt32]()

let queue = dispatch_queue_create("myqueue", DISPATCH_QUEUE_CONCURRENT)
let serialQueue = dispatch_queue_create("mySerialQueue", DISPATCH_QUEUE_SERIAL)
let serialGroup = dispatch_group_create()

dispatch_apply(src.count, queue) { i in
    let result = arc4random_uniform(UInt32(i)) // Your long-running transformation here
    dispatch_group_async(serialGroup, serialQueue) {
        dst.append(result)
    }
}

// Wait until all append operations are complete
dispatch_group_wait(serialGroup, DISPATCH_TIME_FOREVER)
Run Code Online (Sandbox Code Playgroud)