Grand central dispatch – use ‘sync’ to schedule queues and use with What is the difference between work items marked by wait?
•
Java
I'm learning Apple's GCD and watching the video concurrent programming with GCD in swift 3
At 16:00 in this video, a flag of dispatchworkitem is called Wait, functions and charts all show what I think of myqueue Sync (execute:) exactly
So my question is; What's the difference?
myQueue.sync { sleep(1); print("sync") }
And:
myQueue.async(flags: .wait) { sleep(1); print("wait") }
// NOTE: This Syntax doesn't compile,I'm not sure where the `.wait` flag moved to.
// `.wait` Seems not to be in the DispatchWorkItemFlags enum.
It seems that these two methods block the current thread while waiting for the named queue:
>Complete any current or previous work (if continuous) > complete a given block / work item
My understanding of this must be somewhere. What did I miss?
Solution
. Wait is not a flag in dispatchworkitemflags, that's why
myQueue.async(flags: .wait) { sleep(1); print("wait") }
Do not compile
Wait() is a method of dispatchworkitem, which is just a wrapper_ block_ wait().
/*! * @function dispatch_block_wait * * @abstract * Wait synchronously until execution of the specified dispatch block object has * completed or until the specified timeout has elapsed.
Simple example:
let myQueue = DispatchQueue(label: "my.queue",attributes: .concurrent)
let workItem = DispatchWorkItem {
sleep(1)
print("done")
}
myQueue.async(execute: workItem)
print("before waiting")
workItem.wait()
print("after waiting")
dispatchMain()
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
