4 ms·
Beware, they are different concepts. Threads offer concurrent execution, async (futures) offer concurrent waiting. Loosely speaking, threads make sense for CPU
by repelsteeltje 6mo ago
Beware, they are different concepts.
Threads offer concurrent execution, async (futures) offer concurrent waiting. Loosely speaking, threads make sense for CPU bound problems, while async makes sense for IO bound problems.
- groundzeros2015 6mo agoWhy? You write the same code with async await but with a keyword at the beginning of every function.
- f_devd 6mo agoBecause if you go down the callstack eventually you won't get the await keyword anymore; you'll get the actual 'waiters' and 'wakers' which define your scheduling
- groundzeros2015 6mo agoYeah. The OS handles scheduling and preemption so it’s done for you rather than a call in the stack.
- pkolaczk 5mo agoOnly if you ever deal with one future at a time. But async allows things like awaiting one of N events in a very natural way. Those patterns are much less readable when done with threads.