Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between dispatching to a queue with `sync` and using a work item with a `.wait` flag?

I'm learning about Apple's GCD, and watching the video Concurrent Programming With GCD in Swift 3.

At 16:00 in this video, a flag for DispatchWorkItem is described called .wait, and the functionality and diagram both show exactly what I thought myQueue.sync(execute:) was for.

.wait diagram

So, my question is; what is the difference between:

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.

Seems like both approaches block the current thread while they wait for the named queue to:

  1. Finish any current or prior work (if serial)
  2. Complete the given block/work item

My understanding of this must be off somewhere, what am I missing?

like image 559
SimplGy Avatar asked Jun 29 '16 16:06

SimplGy


People also ask

What is difference between operation queue and dispatch queue?

Here are how operation queues are different from dispatch queues: In operation queues, you can set priority for your operations and also you can add dependencies to the operations which means you can define that some operation execute only after the completion of other operations.

What is a dispatch queue?

Dispatch queues are FIFO queues to which your application can submit tasks in the form of block objects. Dispatch queues execute tasks either serially or concurrently. Work submitted to dispatch queues executes on a pool of threads managed by the system.

What is dispatch work item?

A DispatchWorkItem encapsulates work to be performed on a dispatch queue or within a dispatch group. You can also use a work item as a DispatchSource event, registration, or cancellation handler.

What is GCD and dispatch queue?

With GCD, you add blocks of code or work items to dispatch queues and GCD decides which thread to execute them on. As you structure your code, you'll find code blocks that can run simultaneously and some that should not. This allows you to use GCD to take advantage of concurrent execution.


1 Answers

.wait is not a flag in DispatchWorkItemFlags, and that is why your code

myQueue.async(flags: .wait) { sleep(1); print("wait") }

does not compile.

wait() is a method of DispatchWorkItem and just a wrapper for dispatch_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()
like image 193
Martin R Avatar answered Sep 28 '22 06:09

Martin R