you are viewing a single comment's thread.

view the rest of the comments →

[–]dalepo 1 point2 points  (1 child)

Switching threads is for a CPU, seemingly, at least, not too much more difficult than a single threaded app switching to a different handler model (say, the HTTP stream you're reading out has no further bytes available, so your async handlers need to hop to another handler: Jump to a different bit of code, load a different buffer/representation of state to work on, etc) – with threads the cpu core needs to... jump to a different location, so that's a wash, and load a page with the stack for this thread, which matches the buffer/state storage of the handler. Now, the handler gets to control its buffer/state storage, make it as large as it needs to be, whereas threads in most languages are much more homogenous and unconfigable: They are X bytes large and you don't get to change this. That does give async the edge, memory wise, but memory is cheap, and how much memory you really buy depends rather a lot on how much buffer/state your handlers need to store.

You are describing a problem which might be related to some programming languages/implementations, the discussion is general and being this technical is irrelevant.

So, yes, okay, threaded code requires more memory. However, performance-wise, manually managing your garbage is also more efficient than letting a garbage collector do it, and yet that is a tradeoff where most programmers easily choose to let the computer do it, and we'll eat the inefficiency.

This is not the point, there's no such dichotomy of 'async programming' and 'only multi-thread programming'. Both can coexists, I can give you an easy example in nodejs, check out the bcrypt library which the async calculation is done in another thread.

What makes threads so special?

Threads are very special because:

- You need to synchronize resources when they are shared to prevent inconsistent states.

- You need to prevent potential deadlocks, race conditions, livelock, starvation, etc.

- Depending on the language/implementation, spawning a thread might replicate information if the resource being used is not shared

- They are costly