1

I've heard a lot about apple's famous dispatch queues and the GCD but today was the first time I decided to understand exactly what is going on, so I started reading Concurrency Programming Guide and this paragraph caught my eye.

If you have two tasks that access the same shared resource but run on different threads, either thread could modify the resource first and you would need to use a lock to ensure that both tasks did not modify that resource at the same time. With dispatch queues, you could add both tasks to a serial dispatch queue to ensure that only one task modified the resource at any given time. This type of queue-based synchronization is more efficient than locks because locks always require an expensive kernel trap in both the contested and uncontested cases, whereas a dispatch queue works primarily in your application’s process space and only calls down to the kernel when absolutely necessary.

What I understand is that they are suggesting executing two tasks serially to avoid use of locks, which can be done the same way in threads. For example in Java you can put two functions in your thread's runnable and the thread will execute them serially and you will not need locks in that case two.

Now my question is that am I missing something ?

  • if you put 2 functions in your thread's runnable, aren't you running in 1 thread? – gbjbaanb Aug 12 '13 at 8:09
  • yes, but why running them sequentially in queues instead ? they seems the same to me – Mr.Me Aug 12 '13 at 8:14
1

No, I don't think you're missing anything. If you don't run things in parallel then you don't (necessarily) need locking. The distinction, insofar as there is one, is that the GCD interface makes it pretty easy to do.

1

The difference is that with a serial dispatch queue you can't just perform one task after the other. You can do a lot more. Any number of sources can dispatch any number of tasks to that serial queue at any time, and they will be executed one after the other.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.