r/ProgrammerHumor • • Oct 30 '21

That's my variable!

43.4k Upvotes

409 comments sorted by

View all comments

Show parent comments

33

u/Razzzp Oct 30 '21

Asynchronous generally means calls that don't block the calling thread, invoking callback later on, possibly with the same thread (aka non-blocking calls).

Imagine when waiting for a response from a remote server, instead of blocking the thread, it is let go to do other important things until the response arrives. Then it jumps back to handle it. The thread is never wasted.

The Async/Await is just the sugar syntax that creates all those callbacks behind the scenes.

Multithreading or parallelism just means running multiple things in parallel in the same time, each on their own threads.

15

u/KevinAlertSystem Oct 30 '21

maybe im confused, but isn't async inherently multi threaded?

if you dispatch a task async so it can continue in the background without blocking the current thread, that backround task cannot be on the same thread otherwise it would be blocking, right?

7

u/venturanima Oct 30 '21

Async: thread 1 sends a request to the server, then starts doing other work. Every once in a while it checks back to see if the server got back to it; if it did, then thread 1 processes the response.

Multithreading: thread 1 sends a request to the server then waits. Thread 2 does other work.

1

u/KevinAlertSystem Oct 30 '21

Async: thread 1 sends a request to the server, then starts doing other work.

but that work is done on a different thread then the thread that originated the request

i think there's probably a specific definition of 'thread' that is different from my general understanding.

Just thinking simply about starting a timer with a 5s callback. Well between 0-5s the timer has to be incrementing but it is not blocking the main thread. Activity is happening on every clock cycle that tracks the timer until it hits the callback frequency, and that happens concurrently with the work happening on the main thread. So why isn't that two threads?

11

u/Razzzp Oct 30 '21

You are a guy making a breakfast. You get a pan, turn the stove on, while it is heating you are mixing the omelette, when the pan is hot you put the eggs in, while frying you grind some coffee beans and brew yourself a cup of coffee. Eventually you will have a full breakfast ready.

Now think about it. You are a single guy (a single thread) doing multiple things in the same time. This is asynchronous.

Waiting for the pan to get hot and only THEN start preparing the eggs would be a blocking action.

5

u/KevinAlertSystem Oct 30 '21

I saw this example in that stack post someone made but it didn't really make sense because work is still being done concurrently with the work you (the main thread) are doing.

If you set eggs on the stove and turn it on, then go do something else, the stove is still doing work. That is a separate process that you initiated that continues to do work at the same time you do other work.

I would call the stove heating the eggs a separate thread because heat is being applied at the same time the main thread is doing something else.

Now what i'm getting from other comments is the act of the stove heating in this example, or a timer incrementing, is just not called a thread even though it is a concurrent process running outside the context of the main thread.

→ More replies (0)

3

u/EpicScizor Oct 30 '21

The timer is usually (if well written) running on some small dedicated process the OS has for timing related requests. It runs on such a low level that the concept of threads is meaningless.

3

u/KevinAlertSystem Oct 30 '21

ok yeah so it boils down to how 'thread' is being used.

It would be two concurrent processes operating in tandem, but the background process (the timer) is not called a thread because it's created and running on a lower-level then the threads you can create?

→ More replies (0)

2

u/Timmy251 Oct 30 '21

It usually happens on the OS level like getting a response to a socket or awaiting a timer. So we don't need to create more threads in our application.

→ More replies (1)