grand-central-dispatch

Why doesn't .async on a concurrent queue in a for loop behave the same as DispatchQueue.concurrentPerform?

▼魔方 西西 提交于 2021-01-07 02:54:02
问题 import Dispatch class SynchronizedArray<T> { private var array: [T] = [] private let accessQueue = DispatchQueue(label: "SynchronizedArrayAccess", attributes: .concurrent) var get: [T] { accessQueue.sync { array } } func append(newElement: T) { accessQueue.async(flags: .barrier) { self.array.append(newElement) } } } If I run the following code, 10,000 elements are appended to the array as expected even if I am reading concurrently: DispatchQueue.concurrentPerform(iterations: 10000) { i in _

Max values of semaphore?

只愿长相守 提交于 2021-01-01 06:49:08
问题 For example, there is a 1000 times loop. What's the max value to make it fast, effective, and not lead to a deadlock? let group = DispatchGroup() let queue = DispatchQueue(label: "com.num.loop", attributes: .concurrent) let semaphore = DispatchSemaphore(value: 4) for i in 1...1000 { semaphore.wait() group.enter() queue.async(group: group, execute: { doWork(i) group.leave() semaphore.signal() }) } group.notify(queue: DispatchQueue.main) { // go on... } 回答1: A couple of observations: You never

Max values of semaphore?

こ雲淡風輕ζ 提交于 2021-01-01 06:48:27
问题 For example, there is a 1000 times loop. What's the max value to make it fast, effective, and not lead to a deadlock? let group = DispatchGroup() let queue = DispatchQueue(label: "com.num.loop", attributes: .concurrent) let semaphore = DispatchSemaphore(value: 4) for i in 1...1000 { semaphore.wait() group.enter() queue.async(group: group, execute: { doWork(i) group.leave() semaphore.signal() }) } group.notify(queue: DispatchQueue.main) { // go on... } 回答1: A couple of observations: You never

What is the difference in approach to create DispatchQueue Swift3

旧巷老猫 提交于 2020-12-29 06:24:17
问题 I am a rookie in Swift, and there is such misunderstandings what is the difference how to create dispatch queue sample 1 let backgroundQueue = DispatchQueue(label: "com.app.queue", qos: .background, target: nil) backgroundQueue.async { print("Dispatched to background queue") } sample 2 let backgroundQueue = DispatchQueue.global() backgroundQueue.async { print("Dispatched to background queue") } as far as I understand this two approaches do the same or for example this approach DispatchQueue

How to stop execution of a running background thread from main thread on swift while using DispatchQueue

爱⌒轻易说出口 提交于 2020-12-28 07:52:47
问题 DispatchQueue.global(qos: .background).async { //This will run on the background queue self.writeValue(tag: GlobalData.WRITE_DATA, data: getDataForWrite(1) ) self.writeValue(tag: GlobalData.WRITE_DATA, data: getDataForWrite(2) ) self.writeValue(tag: GlobalData.WRITE_DATA, data: getDataForWrite(3) ) self.writeValue(tag: GlobalData.WRITE_DATA, data: getDataForWrite(4) ) self.writeValue(tag: GlobalData.WRITE_DATA, data: getDataForWrite(5) ) // ..... DispatchQueue.main.async { //This will run on

How to stop execution of a running background thread from main thread on swift while using DispatchQueue

好久不见. 提交于 2020-12-28 07:51:05
问题 DispatchQueue.global(qos: .background).async { //This will run on the background queue self.writeValue(tag: GlobalData.WRITE_DATA, data: getDataForWrite(1) ) self.writeValue(tag: GlobalData.WRITE_DATA, data: getDataForWrite(2) ) self.writeValue(tag: GlobalData.WRITE_DATA, data: getDataForWrite(3) ) self.writeValue(tag: GlobalData.WRITE_DATA, data: getDataForWrite(4) ) self.writeValue(tag: GlobalData.WRITE_DATA, data: getDataForWrite(5) ) // ..... DispatchQueue.main.async { //This will run on

DispatchQueue threads don't always set the correct results

别等时光非礼了梦想. 提交于 2020-12-13 03:09:07
问题 Am trying on coding game MineSweeper, the following code is to set the numbers around landmines. For a test, I choose the minimum level 9 x 9 with 10 landmines. For a faster performance, I tried to use more threads when setting numbers, but one day I found that it doesn't always give the correct number arrangements, I made a loop to create it 1000 times and found that 20 ~ 40 out of 1000 are wrong. Here are several wrong results, "*" represents landmine, "0" means no landmine around wrong