Async in Swift 401
In previus post, I have written about race conditions and how to solve them. Today, I will write about other common multi-threading problems like Deadlock and Starvation. They are not very common in iOS environment. But you might still face them.
Deadlock
Here is wiki’s definition
In concurrent computing, a deadlock is a state in which each member of a group is waiting for another member, including itself, to take action, such as sending a message or more commonly releasing a lock.
In previous post, we have used NSLock to solve race condition problem. If you are not careful when using NSLock, you might create Deadlock. When you acquire a lock, you must release it. Otherwise, some task will wait indefinitely.
This is an example of deadlock;
This code will wait indefinitely because we try to acquire same lock again before releasing it. Execution stops at line 21 (lock.lock()). We were going to release the lock in line 17. But it will never be executed. Well, this is a design issue. In this example, it can be prevented by changing the design. But it might not be possible to do that. You can solve this issue by using NSRecursiveLock. Just change the line 3 as below.
var lock = NSRecursiveLock()
NSRecursiveLock is a special lock. It allows you to acquire same lock multiple times by the same thread.
Apple Documentation:
A lock that may be acquired multiple times by the same thread without causing a deadlock.
There are some other useful locks. I advice you to take a look. From here.
A lock that multiple applications on multiple hosts can use to restrict access to some shared resource, such as a file.
A lock that can be associated with specific, user-defined conditions.
Deadlock caused by DispatchQueue
Here is a sample code; how can a DispatchQueue cause a Deadlock
You can prevent this deadlock by using a concurrent queue or making getFriends func async.
This is the output
getUser(id:) entered queue...
Starvation
Starvation is another topic in multi-threading. It means, some task never gets chance to do its work and therefore cannot finish its job. I am not going into this in detail. Because, it is not something you can encounter in your daily life. If you are using DispatchQueue, you will probably be safe.
Here is wiki’s definition
In computer science, resource starvation is a problem encountered in concurrent computing where a process is perpetually denied necessary resources to process its work.
We have solved some common problems in multi-threading. We have used different swift API’s to solve them. I hope you understand what multi-threading is, why you need it and If you face a multi-threading problem, how to solve them. Now, this is the last part of asynchronous programming series, you can find other articles related to topic below. In next post, I will write about DispatchWorkItem and DispatchGroup. I’ll build a sample app about how to use them in a real world scenario. Thank you for reading. Any feedback would be appreciated. Please, get in touch if you have any question.