What is the difference between asynchronous and multithreading when we consider only one core

Viewed 52

A thread is a certain set of instructions to be executed.

A function is a certain set of instructions to be executed.

With one (let's call it worker) worker per core, for a single core we have only 1 worker.

When we say concurrent multithreading, meaning multiple threads on single core, only one thread will be executed at a time. So the worker will execute some instructions from 1 thread then move onto another depending on how much time each thread has been assigned.

In asynchronous programming, we have one main thread running which executes some instructions from one function, then some from another.

In both cases, we have a single worker, executing part of a certain set of instructions, then moving on to another set of instructions. What is the difference?

1 Answers

I think that your are looking for the difference between two ways of achieving concurrency: OS threads and virtual threads (also called green threads).

OS threads are managed and scheduled by the OS. They provide concurrency at the instruction level: some instruction from a thread are executed, then another one is allowed to run some instructions, etc.

Virtual threads are an emulation of threading at the application level. Virtual threads require a runtime to manage and schedule them (sometimes called a run loop or event loop). Virtual threads provide concurrency at (roughly) the function level: some functions are executed, then at a specific point called a suspension point other functions are allowed to be run by the event loop.

Async-await is a syntactic feature of a given programming language and does not provide concurrency per se. It was designed to simplify the expression of asynchronous code by allowing asynchronous functions to be declared and called like synchronous ones. However, other syntaxes exist: callbacks, futures and promises, dispatch queues, etc.

On the contrary, threads are a concurrency primitive provided by the OS. As said at the beginning of this answer, they provide concurrency at the instruction level, in a preemptive manner (the OS scheduler can suspend a thread at any point) and at the OS level.

Related