codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🦀

Rust

23 / 58 topics
23Concurrency24Threads25Channels
Tutorials/Rust/Concurrency
🦀Rust

Concurrency

Updated 2026-05-15
10 min read

Concurrency

Introduction

Concurrency is the ability of a program to execute multiple tasks simultaneously. In the context of programming, this often involves running multiple threads or processes that can operate independently and concurrently. Rust provides robust support for concurrency through its ownership model, which helps prevent data races and other common concurrency issues.

Rust's concurrency model is built around the concept of "safe concurrency," where the language ensures memory safety without requiring a garbage collector. This is achieved through a combination of ownership, borrowing, lifetimes, and the standard library's concurrency primitives like threads and channels.

Concept

Concurrency in Rust is primarily managed through threads. A thread is a lightweight unit of execution that can run concurrently with other threads. Each thread has its own stack but shares the same heap memory. This shared memory model requires careful management to avoid data races, where multiple threads access the same data simultaneously without proper synchronization.

Rust's ownership system plays a crucial role in concurrency by ensuring that each piece of data is owned by exactly one thread at a time. When data needs to be shared between threads, Rust provides mechanisms like Arc (Atomic Reference Counting) and Mutex (Mutual Exclusion) to safely manage access.

Examples

Basic Thread Creation

Let's start with a simple example that demonstrates how to create and run a thread in Rust.

Rust
1use std::thread;
2use std::time::Duration;
3
4fn main() {
5 let handle = thread::spawn(|| {
6 for i in 1..10 {
7 println!("hi number {} from the spawned thread!", i);
8 thread::sleep(Duration::from_millis(1));
9 }
10 });
11
12 for i in 1..5 {
13 println!("hi number {} from the main thread!", i);
14 thread::sleep(Duration::from_millis(1));
15 }
16
17 handle.join().unwrap();
18}

In this example, we create a new thread using thread::spawn. The closure passed to spawn is executed in the new thread. We then join the thread back to the main thread using handle.join(), which waits for the spawned thread to finish.

Sharing Data Between Threads

To share data between threads, Rust provides several synchronization primitives. One of the most common is Arc (Atomic Reference Counting) combined with Mutex.

Rust
1use std::sync::{Arc, Mutex};
2use std::thread;
3
4fn main() {
5 let counter = Arc::new(Mutex::new(0));
6 let mut handles = vec![];
7
8 for _ in 0..10 {
9 let counter = Arc::clone(&counter);
10 let handle = thread::spawn(move || {
11 let mut num = counter.lock().unwrap();
12 *num += 1;
13 });
14 handles.push(handle);
15 }
16
17 for handle in handles {
18 handle.join().unwrap();
19 }
20
21 println!("Result: {}", *counter.lock().unwrap());
22}

In this example, we use Arc to create a reference-counted smart pointer that can be shared between threads. The Mutex ensures that only one thread can access the data at a time, preventing data races.

What's Next?

Now that you have a basic understanding of concurrency in Rust, you can explore more advanced topics such as asynchronous programming with async/await, using channels for communication between threads, and leveraging Rust's powerful concurrency primitives to build efficient and safe concurrent applications.


PreviousPanic!Next Threads

Recommended Gear

Panic!Threads