From the course: iOS Development: Threading and Grand Central Dispatch

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Avoiding deadlocks using dispatch_async

Avoiding deadlocks using dispatch_async

From the course: iOS Development: Threading and Grand Central Dispatch

Start my 1-month free trial

Avoiding deadlocks using dispatch_async

- [Instructor] There isn't one right answer to avoiding deadlocks, but since the introduction of Grand Central Dispatch, using threads is safer because it's handled by the system. But there are some techniques you can use to avoid deadlocks, such as moving some tasks from the main thread. Another remedy is not calling the dispatch.sync function from a task that is executing on the same queue that you pass to your function call. Doing so will deadlock the queue. If you need to dispatch to the current queue, do so asynchronously using the dispatch.async function. You can also use NSRecursiveLocks to avoid deadlocks. NSRecursiveLock is a lock that can be acquired multiple times by the same thread without causing a deadlock. The locks and unlocks have to be balanced. When there's a balance between the locks and unlocks, the lock is released for other threads to acquire it. Another option will be to use a Scheduler provided in…

Contents