Day 27: Learn about threads and message passing.

Learner, Love to make things simple, Full Stack Developer, StackOverflower, Passionate about using machine learning, deep learning and AI
Search for a command to run...

Learner, Love to make things simple, Full Stack Developer, StackOverflower, Passionate about using machine learning, deep learning and AI
No comments yet. Be the first to comment.
Move beyond traditional RESTful thinking. Learn how to design APIs specifically for MCP (Model Context Protocol) servers. This guide covers the shift in mindset, a practical OpenAPI 3.1 example, and a Spring Boot implementation to make your services ...

Extending Kestra to Every Corner of Your Data Stack. Introduction: The Power of Plugins Imagine you're a master chef. You don't just have one knife - you have specialized tools for every task: a paring knife for delicate work, a chef's knife for chop...
Mastering Complex Orchestration Scenarios. Introduction: The Orchestrator's Toolkit Imagine you're conducting a symphony. You don't just wave your baton - you cue sections, adjust tempo, handle surprises, and ensure harmony. That's what advanced work...
From Data Extraction to Loading - A Practical Guide Introduction: Why ETL Still Matters in the Modern Data Stack Remember when data engineering was "extract, transform, load"? Some say ETL is dead, replaced by ELT, reverse ETL, and data mesh. But her...
Building Blocks of Declarative Orchestration. Introduction: The Power of Simplicity Imagine trying to build a house without understanding bricks, beams, and blueprints. That's what using an orchestration tool without understanding its core concepts f...
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.
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.
Rust's std::thread::spawn function creates a new thread and executes a closure or function in that thread.
use std::thread;
fn main() {
let handle = thread::spawn(|| {
println!("Hello from a thread!");
});
handle.join().expect("Thread panicked!");
println!("Thread execution completed.");
}
The join method blocks the current thread until the thread represented by the JoinHandle completes its execution.
Rust uses channels to facilitate communication between threads, allowing safe data transfer and synchronization.
Channels are created using std::sync::mpsc, enabling sending and receiving messages between threads.
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!");
}
The send method sends a message over the channel, while recv receives a message, blocking until data is available.
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.
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.");
}
Each thread in Rust has a unique identifier or Thread ID (tid). Retrieving the tid can be useful for debugging or identification purposes.
use std::thread;
fn main() {
let handle = thread::spawn(|| {
println!("Thread ID: {:?}", thread::current().id());
});
handle.join().expect("Thread panicked!");
}
Rust allows sharing data between threads using Arc (Atomic Reference Counting) and Mutex or RwLock for synchronization. Here's an 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());
}
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://www.youtube.com/@maheshwarligade
\==========================**=========================
If this article adds any value to you then please clap and comment.
Let’s connect on Stackoverflow, LinkedIn, & Twitter.