Search
 
SCRIPT & CODE EXAMPLE
 

RUST

multithreading rust 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..10 {
        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().unwrap();
    }

    println!("Result: {}",counter.lock().unwrap());

    return;
}
Comment

PREVIOUS NEXT
Code Example
Rust :: rust read splited string as vector 
Rust :: create a new rust project folder with cargo 
Rust :: rustlang char array 
Rust :: rust vector insert 
Rust :: input output rust 
Rust :: rust string interpolation 
Rust :: rust vec of generics types 
Rust :: rust convert binary data into ASCII text using Base64 RFC 4648. 
Rust :: rust BMI 
Rust :: slice indices are of type usize rust 
Rust :: declare an array with signle value Rust 
Rust :: rust match wildcard 
Rust :: rust How to use if let statement with conditions? 
Rust :: update a value in hashmap in rust 
Rust :: rust error: failed to run custom build command for python3-sys 
Rust :: convert i32 to usize rust 
Lua :: if string contains lua 
Lua :: lua pcall 
Lua :: roblox interpolate color 
Lua :: luau table find 
Lua :: for i in pairs lua 
Lua :: lua infinite 
Lua :: roblox table find 
Lua :: wait for player character roblox 
Lua :: lua string 
Lua :: pico8 draw sprite 
Lua :: Roblox Studio Mouse Shaking 
Matlab :: matlab symbolic function 
Matlab :: scilab plot 2d function 
Basic :: bash catch ctrl-c in a script 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =