Skip to main content

Difference Between Synchronous And Asynchronous Task Execution


To start, with bare fundamentals...

Synchronous task execution means that the task dispatched will return control and continue executing when the dispatched task had finished.

Asynchronous task execution, on the other hand, means that the dispatched task will return control immediately, and the dispatched task will continue executing elsewhere (in background).

Ok so what does this mean on a deeper level? In iOS, there are various ways to "dispatch" part of a program (task) to the background and return control to the executing program immediately (asynchronously). Program will continue to execute on it's path (Thread), while the dispatched task is executing somewhere else (on a different thread). If something needs to be written to or read from the critical section in code, task can be dispatched synchronously, so the exectuion will continue once the dispatched task finishes.

Synchronous execution is running on the run loop of the same thread that dispatched the task (since everything is being blocked until dispatched task finishes and returns).

Asynchronous execution is running on the run loop of some other background thread.

In simple terms, Thread is a path of a program execution. In iOS, each Thread contain his own Run Loop.

Run loop is the event processing loop that schedules the work for the thread, proccesses input sources and puts the thread to sleep when there is none.

Comments