Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Mutexes and Shared State | Rust Fundamentals lesson | BraveProgrammer
  1. Courses
  2. /
  3. Rust Fundamentals

Lesson 37 of 50

·rust

Mutexes and Shared State

20 minutes

Course progress74%

Mutexes and Shared State

Arc<Mutex<T>> lets multiple threads share mutable data safely.

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

fn main() {
    let counter = Arc::new(Mutex::new());
      = [];
       .. {
          = Arc::(&counter);
          = thread::( || {
              = ctr.().();
            *num += ;
        });
        handles.(handle);
    }
       handles { h.().(); }
    (\
0
let
mut
handles
vec!
for
_
in
0
5
let
ctr
clone
let
handle
spawn
move
let
mut
num
lock
unwrap
1
push
for
h
in
join
unwrap
println!
"Final count: {}\", *counter.lock().unwrap()); }
Make it stickPractice what you've learned1 quiz · 1 practical test · Optional activities from Rust FundamentalsBrowse activities
Previous lessonChannels and Message PassingContinue to lesson 38Unsafe RustBack to Rust Fundamentals curriculum