https://github.com/rubnsbarbosa/sample-threads
rust sample threads 🦀
https://github.com/rubnsbarbosa/sample-threads
concurrency multithreading mutex-lock rust thread-safety
Last synced: 2 months ago
JSON representation
rust sample threads 🦀
- Host: GitHub
- URL: https://github.com/rubnsbarbosa/sample-threads
- Owner: rubnsbarbosa
- License: mit
- Created: 2024-06-15T11:18:50.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2024-06-16T02:02:01.000Z (12 months ago)
- Last Synced: 2025-03-20T17:59:26.690Z (2 months ago)
- Topics: concurrency, multithreading, mutex-lock, rust, thread-safety
- Language: Rust
- Homepage:
- Size: 15.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Concurrency and Parallelism with Rust
This repository contains a collection of Rust projects demonstrating some concepts in concurrency and parallelism. Each project is organized as a separate
package within a Cargo workspace.### Getting Started
To get started with these projects, ensure you have [Rust](https://www.rust-lang.org/) installed on your machine. You can install Rust using `rustup` run the
following command in your terminal, then follow the onscreen instructions.```shell
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
```### Executing the code
under the root workspace execute the cargo command below
```shell
cargo run --bin # e.g., _00-new-thread-with-spawn
```or, inside the project directory `cargo run`
### Projects overview
Here you can find an overview about the current codes.
#### _00-new-thread-with-spawn
In this project we create multiple threads using the **thread::spawn** function from the standard library. Each thread print "hey, {thread_identifier} either
from main or spawn thread". We also learn how to keep threads alive until all of they are all executed.```rust
let handle = thread::spawn(move || {
println!("Hey, from thread {}", i);
});
```#### _01-sum-array-in-parallel
This project demonstrates how to sum all the elements of an array in parallel using multiple threads. It also use data decomposition by dividing the vector into
chunks.```rust
for chunck in vector.chunks(chunk_vec_size) {
let chunk = chunck.to_owned();
vec_threads.push(thread::spawn(move || -> i32 {
chunk.iter().sum()
}));
}
```#### _02-thread-safety-arc-mut
This project implements a thread-safe counter using a `Mutex` to ensure safe increments among multiple threads and `Arc` used to share ownership of the
`Mutex`. Mutex ensures that multiple threads can safely increment the counter without causing race conditions.See [Mutex](https://doc.rust-lang.org/std/sync/struct.Mutex.html)
* Mutex allows only one thread to access the counter at a time;
* Each thread gets a clone of the Arc pointer, incrementing the reference count;
* Before accessing the counter, each thread locks the Mutex using `lock()`. This ensures that only one thread can modify the counter at a time;
* After all threads have finished, the counter result is printed.#### _03-producer-consumer-buffer
The producer-consumer problem is a classic example of a multi-threading problem where a fixed-size buffer is shared between producer threads that generate data
and consumer threads that process data.## License
This project is licensed under the MIT License. See the [LICENSE](https://github.com/rubnsbarbosa/sample-threads/tree/main?tab=MIT-1-ov-file) file for details.