
From Futures to Runtimes: How Async Rust Actually Works
Introduction to Asynchronous Programming In synchronous programming, tasks execute linearly one after another on a single thread. fn synchronous () { println! ( "1st" ); println! ( "2nd" ); println! ( "3rd" ); } For many situations this works great, but if a single task in the chain is slow the entire program halts as we wait for it to finish. This is called "blocking" the thread. use std :: time :: Duration :: from_secs ; use std :: thread :: sleep ; fn synchronous_blocking () { println! ( "1st" ); sleep ( from_secs ( 3 )); // this blocks for 3 seconds println! ( "2nd" ); println! ( "3rd" ); } Asynchronous programming allows tasks to yield control while waiting for blocking operations, ensuring that one slow task doesn't halt the entire program's progress. There are two main strategies commonly grouped together under the umbrella of asynchronous programming: concurrency and parallelism . Concurrency is like starting on dinner after putting a load of laundry in. Concurrency is about in
Continue reading on Dev.to
Opens in a new tab



