tarun_sharma
tarun_sharma

Reputation: 871

Dispatch task on main queue sync from custom serial queue

I have come across a very interesting problem related to queue dead lock in iOS. Any way to avoid this?

Consider this:

  1. Create a custom serial queue.
  2. Dispatch some task (#1) on this serial queue asynchronously.
  3. This async task (#1) on dispatches some task (#2) onto main queue sync.
  4. Main queue dispatches some task (#3) onto serial queue sync.
  5. Result - DeadLock

Below is the sample code for this.

Upvotes: 1

Views: 397

Answers (1)

Mohammad Sadiq
Mohammad Sadiq

Reputation: 5241

From Apple

Important: You should never call the dispatch_sync or dispatch_sync_f function from a task that is executing in the same queue that you are planning to pass to the function. This is particularly important for serial queues, which are guaranteed to deadlock, but should also be avoided for concurrent queues.

Its a guaranteed dead lock. The only way to avoid this is to not implement it this way.

Upvotes: 1

Related Questions