liveBook · Manning

Synchronous call

As explained in Rust Servers, Services, and Apps

Synchronous programming executes an operation and waits for it to complete before continuing with subsequent work.

Synchronous programming completes a called operation before continuing with subsequent work. If that operation blocks while waiting for a file or database, the current thread waits too. section 10.1 section 10.2

Two sequential reads

In src/main.rs, main() calls read_from_file1(), prints its result, then calls read_from_file2(). Each function uses std::thread::sleep to simulate a delay. With waits of four and two seconds, the program waits six seconds in total: the second call cannot begin while the first is unfinished. section 10.2

use std::thread::sleep;
use std::time::Duration;
 
fn main() {
    println!("Hello before reading file!");
    let file1_contents = read_from_file1();     #1
    println!("{:?}", file1_contents);
    println!("Hello after reading file1!");
    let file2_contents = read_from_file2();     #2
    println!("{:?}", file2_contents);
    println!("Hello after reading file2!");
}
 
// function that simulates reading from a file
fn read_from_file1() -> String {
    sleep(Duration::new(4, –));
    String::from("Hello, there from file 1")
}
 
// function that simulates reading from a file
fn read_from_file2() -> String {
    sleep(Duration::new(2, –));
    String::from("Hello, there from file 2")
}

The Rust listing shows both calls in order, with a blocking sleep inside each function. section 10.2

When independent work need not wait, multithreading can run a blocking task on another operating-system thread. Async processing instead lets a runtime such as Tokio schedule another task while one waits for I/O. Neither changes what the synchronous call itself does: its caller waits for a result before proceeding. section 10.1 section 10.2

How Rust Servers, Services, and Apps covers it

How deep it goes
Defines waiting at a function call and works through simulated file delays; stops short of synchronous implementation internals.
Examples
Uses a Rust main() calling simulated file reads with four- and two-second sleeps, producing a six-second total delay.
What it assumes
Starts with a small Rust function call and defines synchronous waiting before introducing concurrent alternatives.
What you can do after
Diagnose/debug with it when sequential blocking calls make independent operations wait for one another.
How it connects
Connects blocking I/O and thread sleep to multithreading, async processing, and runtime scheduling.

Also explained in

Compare how 6 books explain synchronous call →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust Servers, Services, and Apps. The section links point to the parts of the book it draws on.