0

I know you would deadlock by doing this on a serial queue, but I haven't found anything that mentions deadlocking by doing it on a concurrent queue. I just wanted to verify it wont deadlock (it doesn't seem like it would, as it would only block one of the threads on the queue and the task would run on another thread on the same queue)

Also, is it true that you can control order of execution by calling dispatch_sync on a concurrent queue? (It's mentioned here) I don't understand why that would happen, as async vs sync have to do with the caller thread.

Community
  • 1
  • 1
rb612
  • 5,280
  • 3
  • 30
  • 68
  • 1
    Possibly related: http://stackoverflow.com/questions/13962885/why-is-dispatch-sync-on-custom-concurrent-queue-deadlocking. – Martin R Jun 16 '15 at 06:51
  • Thanks, but it's more of a general question rather than a problem I've encountered. – rb612 Jun 16 '15 at 06:53
  • Unless you can absolutely guarantee that not all threads will end up waiting on something at the same time then there is a deadlock risk - how can you guarantee that? – Wain Jun 16 '15 at 06:55

1 Answers1

2

This will not deadlock since the dispatched block can start running immediately - it's not a serial queue so it doesn't have to wait for the current block to finish.

But it's still not a good idea. This will block one thread causing the OS to spin up a new one (because it still has free CPU as the thread is sleeping) wasting memory.

Sven
  • 22,475
  • 4
  • 52
  • 71
  • Thank you Sven!! And how about the second part of the question regarding control of execution order? It didn't seem quite right. – rb612 Jun 16 '15 at 06:58
  • 3
    GCD is perfectly entitled to run the block right in the current thread, within the call to `dispatch_sync()`. (It doesn't *have to*, but it can.) – Ken Thomases Jun 16 '15 at 07:28
  • Right, I forgot about that. – Sven Jun 16 '15 at 07:54
  • Thanks! So then how would `dispatch_sync` be used to execute blocks one after another like mentioned here? http://stackoverflow.com/questions/19179358/concurrent-vs-serial-queues-in-gcd – rb612 Jun 16 '15 at 08:23