M
M
MarsFM2019-02-27 17:59:18
Swift
MarsFM, 2019-02-27 17:59:18

Deadlcock in swift?

Guys I just can’t understand daedlock, I understand the race condition, but I can’t understand the deadlock (Please
explain
why when I call DispatchQueue.main.sync {} in the main thread, there is a deadlock?
I understand that sync blocks the thread. But when the task is completed, main thread is free again , why can't the task in DispatchQueue.main.sync
{} be executed?

print("a")
DispatchQueue.main.async {
    DispatchQueue.main.async {
        print("b")
    }
    print("c")
}
print("d")

Why is the answer a, d, c, b ?
Because DispatchQueue.main.async {} goes to the end of the queue? what exactly is going on here?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
doublench21, 2019-02-27
@sportredwhite

This is true not only for the main thread, BUT for all successive queues.
Consider this example:
What we get:
Obviously, every sequential queue waits for the current task to complete before starting the next one. When we try to SYNCHRONOUSLY queue the current task, we get a lock. This is because when we execute the code (3rd line), we do it within the framework of the current task already running in the queue ANDtrying to synchronously add a task. Synchronous means that we have to wait for the current task to complete. But how can we complete the task in which we are, if we ourselves slow it down? This is called mutual blocking. The 2nd line of code waits for the 3rd line to complete, and the 3rd line waits for the 2nd to complete. By lines, I mean the actual blocks of code that we add to the SEQUENTIAL queue.
As for the main thread, this happens implicitly. After all, by default we are already inside the task placed in the main queue. And if we try to do the same as in the 3rd line, we will get a lock.
Apple wrote about it:
With regards:
Let's go in order. Starting from #1 - print "a". After doing #2. Here we ASYNCHRONOUSLY add a task to the queue. This means we are telling the system, hey, put this task on the queue and give me back control of the code, I don't want to wait. Therefore, after #2, #6 is immediately executed. After all, we said that we do not want to wait for the completion of the added task. So he returned control and continued to perform. That is, it outputs the character "d".
Now inside the queue we again add ASYNCHRONOUSLY a new task within the scope of the current task. That is, we also say that the system would transfer control back and we do not want to wait for completion. That is, after adding the task, we immediately execute line #5 - print the character "c".
Since it takes some time to add a task to the queue, plus the time it takes to complete the task itself, all this leads to the fact that the output of line #4 is the last one.
That is, all this time we only asked to add tasks to the queue and continue to execute the following lines of code without waiting for their completion. Here are the characters "a, d, c" ​​and were displayed before the character "b".
I don't know how else to explain it. Maybe someone will correct me.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question