Add code
This commit is contained in:
1
example_2/.gitignore
vendored
Normal file
1
example_2/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
8
example_2/Cargo.toml
Normal file
8
example_2/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "example_2"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
parking_lot = "0.12.3"
|
||||
crossbeam = "0.8.4"
|
||||
21
example_2/src/main.rs
Normal file
21
example_2/src/main.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
use crossbeam::scope;
|
||||
use parking_lot::{Mutex, MutexGuard};
|
||||
|
||||
fn main() {
|
||||
let mut counter = Mutex::new(0);
|
||||
|
||||
scope(|s| {
|
||||
for _ in 0..10 {
|
||||
s.spawn(|_| {
|
||||
for _ in 0..10 {
|
||||
let mut guard: MutexGuard<i32> = counter.lock();
|
||||
*guard += 1;
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
let total: &mut i32 = counter.get_mut();
|
||||
println!("total = {}", *total)
|
||||
}
|
||||
Reference in New Issue
Block a user