4 ms·
is the difference between threads and async/await more than syntax? or language-specific?
by plugin-baby 3y ago
is the difference between threads and async/await more than syntax? or language-specific?
- romanovcode 3y agoYes, they are different in most cases. In general await job is to pass the process to 3rd party e.g. database or http and wait for callback whereas thread job is to launch multiple CPU operations in parallel.
- jokethrowaway 3y agoAwait-async is implemented with a runtime which uses a thread pool or a single thread and allocates work when needed on any thread and waits for IO to yield a result. With threads you just fully control what blocking code is running on a single thread. If you are just running computations (or reading files, as filesystem api are not async) it's simpler to just use threads.
- plugin-baby 3y ago> or reading files, as filesystem api are not async This is interesting - all nodejs file system APIs are async by default.
- zaphar 3y agoThat's because javascript as a whole is async by default. Which really makes me confused as to why javascript didn't just go the route of Go and eliminate the distinction instead of falsely creating it with syntax.
- immibis 3y agoImplementation difference. Threads are usually handled by the kernel and each one has its own stack where you can put anything. And the kernel can switch threads at any time. Async/await has the compiler work out precisely what has to be saved across the marked locations which are the only places context switches can happen. Also it doesn't tell the kernel when it switches context.
- gpderetta 3y agoThreads can be in scheduled in userspace and can also be non-preemptive or have deterministic scheduling.