Skip to content

Commit

Permalink
Add scoped threads example and explanation
Browse files Browse the repository at this point in the history
  • Loading branch information
jolisper committed Apr 6, 2024
1 parent f928bf2 commit b4eb839
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
8 changes: 8 additions & 0 deletions 1. Basics of Rust Concurrency/scoped-threads/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "scoped-threads"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
64 changes: 64 additions & 0 deletions 1. Basics of Rust Concurrency/scoped-threads/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//! # What are the benefits of scoped threads?
//!
//! Scoped threads in Rust allow for the creation of threads with lifetimes
//! tied to the scope they are created in. This provides several benefits:
//!
//! - **Safety**: Scoped threads ensure that all references passed to the child
//! thread are guaranteed to be valid for the duration of the thread's
//! execution. This prevents dangling references and data races that can occur
//! with unscoped threads.
//!
//! - **Ease of use**: By knowing that the data used by the thread won't go out
//! of scope prematurely, developers can write simpler and more predictable
//! concurrent code.
//!
//! - **Access to stack data**: Unlike 'spawn' which requires 'static lifetime
//! for the data it captures, scoped threads can safely access data on the
//! stack of the parent thread. This is particularly useful for borrowing data
//! temporarily.
//!
//! - **No need for 'Arc'**: In many cases, scoped threads remove the need for
//! atomic reference counting (`Arc`) because the compiler can ensure
//! references will outlive the thread's scope. This can lead to performance
//! improvements by avoiding atomic operations.
//!
//! - **Controlled thread lifetime**: When the scope ends, the thread is
//! guaranteed to finish before moving on. This makes it easier to reason
//! about the program state and avoids having to manually join threads.
//!
//! Scoped threads are a powerful feature in Rust that allows for safer and
//! more convenient concurrent programming by leveraging Rust's strong
//! guarantees about lifetimes and borrowing.
//!
#[allow(unused)]
use std::{
cell::{Cell, RefCell, UnsafeCell},
collections::VecDeque,
marker::PhantomData,
mem::{ManuallyDrop, MaybeUninit},
ops::{Deref, DerefMut},
ptr::NonNull,
rc::Rc,
sync::{
atomic::{Ordering::*, *},
*,
},
thread::{self, Thread},
};

fn main() {
let numbers = vec![1, 2, 3, 4, 5];

thread::scope(|s| {
s.spawn(|| {
println!("Thread ID: {:?}", thread::current().id());
for n in &numbers {
println!("{}", n);
}
});
s.spawn(|| {
println!("Thread ID: {:?}", thread::current().id());
println!("length: {:?}", numbers.len());
});
});
}
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

resolver = "2"

members = []
members = ["1. Basics of Rust Concurrency/scoped-threads"]

0 comments on commit b4eb839

Please sign in to comment.