Day 27: Learn about threads and message passing.

Day 27: Learn about threads and message passing.

On Day 27, we'll delve deeper into Rust's threading capabilities and explore message passing using channels, crucial aspects of concurrent programming in Rust.

Threads in Rust

Rust's standard library (std::thread) facilitates multi-threading, allowing concurrent execution of code. Threads enable parallelism and asynchronous tasks, enhancing program performance. Let's dive into Rust's threads.

Creating Threads

Rust's std::thread::spawn function creates a new thread and executes a closure or function in that thread.

Example:

use std::thread;

fn main() {
    let handle = thread::spawn(|| {
        println!("Hello from a thread!");
    });

    handle.join().expect("Thread panicked!");
    println!("Thread execution completed.");
}

Joining Threads

The join method blocks the current thread until the thread represented by the JoinHandle completes its execution.

Message Passing with Channels

Rust uses channels to facilitate communication between threads, allowing safe data transfer and synchronization.

Creating Channels

Channels are created using std::sync::mpsc, enabling sending and receiving messages between threads.

Example:

use std::thread;
use std::sync::mpsc;

fn main() {
    let (sender, receiver) = mpsc::channel();

    let handle = thread::spawn(move || {
        sender.send("Message from the thread").unwrap();
    });

    let received = receiver.recv().unwrap();
    println!("Received: {}", received);

    handle.join().expect("Thread panicked!");
}

Sending and Receiving Messages

The send method sends a message over the channel, while recv receives a message, blocking until data is available.

Threads in Rust: Advanced Usage

Join Handles

Join handles allow threads to wait for another thread to complete its execution using join. Additionally, you can use thread::sleep to simulate computation or delays.

Example:

use std::thread;
use std::time::Duration;

fn main() {
    let handle = thread::spawn(|| {
        for i in 1..=5 {
            println!("Working in thread... {}", i);
            thread::sleep(Duration::from_millis(500));
        }
    });

    handle.join().expect("Thread panicked!");
    println!("Thread execution completed.");
}

Thread IDs

Each thread in Rust has a unique identifier or Thread ID (tid). Retrieving the tid can be useful for debugging or identification purposes.

Example:

use std::thread;

fn main() {
    let handle = thread::spawn(|| {
        println!("Thread ID: {:?}", thread::current().id());
    });

    handle.join().expect("Thread panicked!");
}

Sharing Data between Threads

Rust allows sharing data between threads using Arc (Atomic Reference Counting) and Mutex or RwLock for synchronization. Here's an example:

Example:

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

fn main() {
    let counter = Arc::new(Mutex::new(0));
    let mut handles = vec![];

    for _ in 0..5 {
        let counter = Arc::clone(&counter);
        let handle = thread::spawn(move || {
            let mut num = counter.lock().unwrap();
            *num += 1;
        });
        handles.push(handle);
    }

    for handle in handles {
        handle.join().expect("Thread panicked!");
    }

    println!("Counter: {:?}", *counter.lock().unwrap());
}

Conclusion

Threads and message passing are fundamental concepts in concurrent programming. In Rust, threads enable parallel execution, while channels provide a safe mechanism for inter-thread communication. Understanding how to create threads, manage their execution, and utilize channels for message passing is crucial for building robust and performant concurrent Rust applications.

Happy coding with Rust!

I hope this helps, you!!

More such articles:

https://medium.com/techwasti

https://www.youtube.com/@maheshwarligade

https://www.techwasti.com/

\==========================**=========================

If this article adds any value to you then please clap and comment.

Let’s connect on Stackoverflow, LinkedIn, & Twitter.

Did you find this article valuable?

Support techwasti by becoming a sponsor. Any amount is appreciated!