Wednesday, August 29, 2018

The essence of asynchronous programming

Asynchronous programming is a kind of parallel programming, where the fundamental unit of computation is a task and not a thread.

A typical thread can be in 2 states: blocked and active. It is blocked while waiting access to a shared resource, or waiting information which is going to arrive through input/output devices. It is active when its code is executing on a processor. In the both states, the thread keeps significant amount of memory (typically, 0.5-1 megabyte) for procedure stack. This restricts maximum number of threads which can exist simultaneously on a single computer.

Similarly, an asynchronous task can be in blocked or active state, for the same reasons. But in the blocked state, the task is not bound to a procedure stack. A stack is assigned only when the task is active and is running on a processor. Since the number of processors in contemporary computers is small (less than 100), memory consumption for stacks become insignificant. The memory for the task itself is usually much less than 1 megabyte, so the maximum number of task is orders of magnitude more than maximum number of threads. This allows to write massively parallel algorithms in more natural way than when using threads.

The downside of using tasks vs threads is that the program code become more complicated. Partially, this can be mitigated using async/await syntax constructs, found in such languages as C#, Dart, Ecmascript-262, and Kotlin.

Another difference is that the constructs, used to exchange information between tasks, have different interface and implementation than the constructs for interaction between threads. But for each kind of inter-thread communication, we can implement an analogue inter-task facility. This has significant consequence: a parallel algorithm can be first develop in abstract form, and only at the  implementation stage programmer can chose how to implement this or that activity: as a thread or as an asynchronous task. The final program can contain both threads and tasks, and they can freely interact with each other.

2 comments:

  1. Hi Alexei,
    You have to be interested to view this:
    https://www.youtube.com/watch?v=J31o0ZMQEnI&index=2&list=PLX8CzqL3ArzVnxC6PYxMlngEMv3W1pIkn&t=0s

    ReplyDelete