2

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?

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57
Rammy Rock
  • 23
  • 2
  • If you look into the description of tag "asynchronous", you will find that threading is one of its possible implementation: "Such strategies are usually employed using some combination of event-driven programming and callbacks, and optionally making use of concurrency through coroutines and / or threads." Not sure what sort of difference you are asking about. – Tsyvarev Jul 14 '22 at 23:47
  • I suppose i wasn't clear in the question. If you know C#, I am basically asking the difference between using async/await and using threads. – Rammy Rock Jul 19 '22 at 20:22
  • 1
    If you ask **specifically** about C#, then [edit] your question to reflect that. (And there is already asked question on that topic: https://stackoverflow.com/questions/34680985/what-is-the-difference-between-asynchronous-programming-and-multithreading). In the current form your question looks like about concepts "asynchronous" and "threading" **in general**, and different languages can have different implementation of these concepts, with different properties. – Tsyvarev Jul 19 '22 at 20:58
  • Take a look at the famous [There is no thread](https://blog.stephencleary.com/2013/11/there-is-no-thread.html) article by Stephen Cleary, if you haven't already read it. – Theodor Zoulias Jul 19 '22 at 21:30

1 Answers1

0

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.

Louis Lac
  • 5,298
  • 1
  • 21
  • 36
  • I suppose i wasn't clear in the question. If you know C#, I am basically asking the difference between using async/await and using threads. – Rammy Rock Jul 19 '22 at 20:22
  • Overlapped I/O definitely does not use OS threading as a building block, so async I/O can use that as a foundation and not need multiple threads, OS or virtual. – Ben Voigt Jul 19 '22 at 22:04
  • @BenVoigt I never mentioned I/O nor the OP does. Async I/O is usually implemented using I/O multiplexing APIs provided by the OS such as `select` and `poll`. However, Asynchronous CPU-bound tasks generally require an underlying thread model such as a thread pool in order to not block the event loop. I'll update my response to remove the confusion. – Louis Lac Jul 20 '22 at 09:01
  • @RammyRock Could you please edit your question with more details then ? `async-await` is a syntactic feature which allows an easier expression of asynchronous code while threads are a concurrency primitive provided by the OS. – Louis Lac Jul 20 '22 at 09:07
  • I updated my answer with more details. – Louis Lac Jul 20 '22 at 09:26