Created
October 18, 2020 23:29
-
-
Save yiwu-arbug/4f3991e018ddee284c478362ebe1984d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashMap; | |
use std::sync::Arc; | |
use std::sync::Mutex; | |
use std::sync::RwLock; | |
const MAP_SIZE: usize = 1000000; | |
const THREAD_SIZE: usize = 6; | |
const SECS: u64 = 10; | |
fn main() { | |
test_mutex(); | |
test_rwlock(); | |
} | |
fn test_mutex() { | |
let shared = Arc::new(Mutex::new(HashMap::new())); | |
{ | |
let mut map = shared.lock().unwrap(); | |
for i in 0..MAP_SIZE { | |
map.insert(format!("{}", i), 0); | |
} | |
} | |
let mut handlers = vec![]; | |
for _ in 0..THREAD_SIZE { | |
let shared = shared.clone(); | |
let h = std::thread::spawn(move || { | |
let start = std::time::Instant::now(); | |
let mut ops: usize = 0; | |
let mut sum: usize = 0; | |
while start.elapsed().as_secs() < SECS { | |
let key = format!("{}", ops % MAP_SIZE); | |
{ | |
let map = shared.lock().unwrap(); | |
sum += map[&key]; | |
} | |
ops += 1; | |
} | |
println!("test_mutex read {} ops, sum {}", ops, sum); | |
}); | |
handlers.push(h); | |
} | |
{ | |
let shared = shared.clone(); | |
let start = std::time::Instant::now(); | |
let mut ops: usize = 0; | |
while start.elapsed().as_secs() < SECS { | |
let key = format!("{}", ops % MAP_SIZE); | |
{ | |
let mut map = shared.lock().unwrap(); | |
*map.get_mut(&key).unwrap() += 1; | |
} | |
ops += 1; | |
} | |
println!("test_mutex write {} ops", ops); | |
} | |
for h in handlers { | |
h.join().unwrap(); | |
} | |
} | |
fn test_rwlock() { | |
let shared = Arc::new(RwLock::new(HashMap::new())); | |
{ | |
let mut map = shared.write().unwrap(); | |
for i in 0..MAP_SIZE { | |
map.insert(format!("{}", i), 0); | |
} | |
} | |
let mut handlers = vec![]; | |
for _ in 0..THREAD_SIZE { | |
let shared = shared.clone(); | |
let h = std::thread::spawn(move || { | |
let start = std::time::Instant::now(); | |
let mut ops: usize = 0; | |
let mut sum: usize = 0; | |
while start.elapsed().as_secs() < SECS { | |
let key = format!("{}", ops % MAP_SIZE); | |
{ | |
let map = shared.read().unwrap(); | |
sum += map[&key]; | |
} | |
ops += 1; | |
} | |
println!("test_rwlock read {} ops, sum {}", ops, sum); | |
}); | |
handlers.push(h); | |
} | |
{ | |
let shared = shared.clone(); | |
let start = std::time::Instant::now(); | |
let mut ops: usize = 0; | |
while start.elapsed().as_secs() < SECS { | |
let key = format!("{}", ops % MAP_SIZE); | |
{ | |
let mut map = shared.write().unwrap(); | |
*map.get_mut(&key).unwrap() += 1; | |
} | |
ops += 1; | |
} | |
println!("test_rwlock write {} ops", ops); | |
} | |
for h in handlers { | |
h.join().unwrap(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment