1

Weekly Rust Trivia: How to share state between threads

 9 months ago
source link: https://www.thorsten-hans.com/weekly-rust-trivia-share-state-between-threads/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Weekly Rust Trivia: How to share state between threads

Published Thu, Jul 27, 2023 / by Thorsten Hans / Estimated reading time: 1 min

Rust  / Weekly Rust Trivia

Weekly Rust Trivia is a problem-oriented series of articles that assist developers while learning Rust. Every article solves simple, everyday development tasks using the Rust standard library or leveraging popular and proven crates.

Question: How to share state between threads in Rust?

In Rust, we have the ability to share data between threads by using concurrent data structures and synchronization tools provided by the Rust standard library. The most popular concurrent data structures in Rust include Arc (atomic reference counting) and Mutex (mutual exclusion). These allow multiple threads to access shared data in a safe and synchronized manner.

For demonstration purposes we’ll use a simple vector (vec) and share it between multiple background threads. Each background thread will push data to the state in a controlled and safe manner:

use std::sync::{Arc, Mutex};
use std::thread;
use std::time::SystemTime;

fn main() {
    let shared_state = Arc::new(Mutex::new(vec![]));
    let mut thread_handles = vec![];
    
    for _ in 0..10 {
        let thread_data = Arc::clone(&shared_state);
        let handle = thread::spawn(move || {
            let mut state = thread_data.lock().unwrap();
            let now = SystemTime::now()
                .duration_since(SystemTime::UNIX_EPOCH)
                .unwrap()
                .as_secs();
            state.push(now);
        });
        thread_handles.push(handle);
    }

    // Wait for all threads to complete
    for handle in thread_handles {
        handle.join().unwrap();
    }

    let final_state = shared_state.lock().unwrap();
    println!("State: {:?}", *final_state);
}

When executing the sample above, we will see the following being printed to stdout (Obviously, actual values will differ slightly):

State: [1690192544, 1690192544, 1690192544, 1690192544, 1690192544, 1690192544,
    1690192544, 1690192544, 1690192544, 1690192544]

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK