Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between DISPATCH_QUEUE_CONCURRENT and DISPATCH_QUEUE_SERIAL

I implement the following class:

class GCDStudy {

    func asyncSerial(time:Double){
        let queue = dispatch_queue_create("DISPATCH_QUEUE_SERIAL",DISPATCH_QUEUE_SERIAL)

        dispatch_async(queue){

            var i:Double = 0
            while(i < 3){
                i++
                print("asyncSerial -wait:\(time)-\(i)")

            }
        }

    }

    func asyncConcurrent(time:Double){

        let queue = dispatch_queue_create("DISPATCH_QUEUE_CONCURRENT",DISPATCH_QUEUE_CONCURRENT)

        dispatch_async(queue){
            var i:Double = 0
            while(i < 3){
                i++
                print("asyncConcurrent -wait:\(time)-\(i)")
            }
        }
    }
}

And run as the following:

Run A:

gCDStudy = GCDStudy()
gCDStudy.asyncSerial(1)
gCDStudy.asyncSerial(2)

Run B

vgCDStudy2 = GCDStudy()
gCDStudy2.asyncConcurrent(1)
gCDStudy2.asyncConcurrent(2)

And the result RunA:

asyncSerial -wait:1.0-1.0
asyncSerial -wait:2.0-1.0
asyncSerial -wait:1.0-2.0
asyncSerial -wait:2.0-2.0
asyncSerial -wait:1.0-3.0
asyncSerial -wait:2.0-3.0

The result of RunB:

asyncSerial -wait:1.0-1.0
asyncSerial -wait:2.0-1.0
asyncSerial -wait:1.0-2.0
asyncSerial -wait:2.0-2.0
asyncSerial -wait:1.0-3.0
asyncSerial -wait:2.0-3.0

These results are the same, what is the different between these?

Sometimes these results are different.

Thanks

like image 588
Leo Avatar asked Nov 10 '15 04:11

Leo


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 DispatchQueue?

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 serial and concurrent queue?

async { // this is another task added to the same queue // this queue now has two tasks } And it's obvious mentioning that concurrent simply means at the same time with other things and serial means one after the other (never at the same time).

Is Main queue serial or concurrent?

The main queue in your app is serial. All the global pre-defined queues are concurrent. Any private dispatch queue you create is serial by default but can be set to be concurrent using an optional attribute as discussed in part 1.


1 Answers

Serial queues execute one task at a time in the order in which they are added to the queue whereas concurrent queues execute one or more tasks concurrently. An important thing to note is that, even for concurrent queues, tasks are started in the order in which they were added to the queue. Because of that, in your case, you'll probably see no change in the output as print will be executed pretty fast by each thread in the concurrent queue so that it'll seem as if they were executed sequentially.

If you expect to see a difference, maybe you might try updating your code as follows:

class GCDStudy {

  func asyncSerial(time: Double) {
    let queue = dispatch_queue_create("DISPATCH_QUEUE_SERIAL", DISPATCH_QUEUE_SERIAL)

    for i in 0..<3 {
      dispatch_async(queue) {
        print("asyncSerial -wait:\(time)-\(i)")
      }
    }
  }

  func asyncConcurrent(time: Double) {
    let queue = dispatch_queue_create("DISPATCH_QUEUE_CONCURRENT", DISPATCH_QUEUE_CONCURRENT)

    for i in 0..<3 {
      dispatch_async(queue) {
        print("asyncConcurrent -wait:\(time)-\(i)")
      }
    }
  }
}
like image 102
Ozgur Vatansever Avatar answered Sep 17 '22 08:09

Ozgur Vatansever