我有关于检查和设置数组中的元素的简单问题,但我想在多线程的异步任务中执行此操作 .

override func viewDidLoad() {
          super.viewDidLoad()
          test()
        }


        let group_dis = DispatchGroup()
        var sum : [Int] = [Int] (repeating: 0, count: 10_000)
        func test() {
            self.group_dis.enter()
            f1(str: "thread 1", min: 0, max: 5000)

            self.group_dis.enter()
            f1(str: "thread 2", min: 100, max: 2699)

            self.group_dis.enter()
            f1(str: "thread 3", min: 5301, max: 9999)

            self.group_dis.wait()
            print(self.sum)
        }
        func f1(str: String, min : Int,  max : Int) {
            let q = DispatchQueue.global(qos: .userInteractive)
            q.async {
                print("running ..... \(str) ")
                for i in min..<max {
                    if (self.sum[i] < i) {
                        self.sum[i] = i
                    }
                }
                self.group_dis.leave()
            }
        }

错误:TestDispatchQueueGroup(67208,0x700000c2c000)malloc: *** error for object 0x7fb4e1022800: pointer being freed was not allocated TestDispatchQueueGroup(67208,0x700000c2c000) malloc: *** 在malloc_error_break中设置断点以进行调试

我知道它崩溃了,因为在多个线程上访问内存单元时发生冲突 . 但这是测试,我的大型项目,我试图找到一个解决方案来执行相同阵列上的并行任务 .

谢谢你的任何建议 .

enter image description here