Concurrency – Apple Dispatch Queue vs Threads

concurrencyjavaobjective c

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 ?

Best Answer

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.

Related Topic