Created
November 4, 2015 22:40
-
-
Save therustmonk/548180c57c0919015354 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::sync::{Arc, Mutex}; | |
use std::sync::atomic::{AtomicUsize, Ordering}; | |
use std::thread; | |
use std::net; | |
use std::io::Read; | |
fn main() { | |
let listener = Arc::new(Mutex::new(0)); | |
let counter = Arc::new(AtomicUsize::new(0)); | |
let mut threads: Vec<_> = Vec::with_capacity(10000); | |
for i in 0..10000 { | |
let listener = listener.clone(); | |
let counter = counter.clone(); | |
let t = thread::spawn(move || { | |
let _ = listener.lock(); | |
std::thread::sleep(std::time::Duration::new(1000, 0)); | |
println!("{}", i); | |
counter.fetch_add(1, Ordering::Relaxed); | |
}); | |
threads.push(t); | |
} | |
std::thread::sleep(std::time::Duration::new(10, 0)); | |
println!("{:?}", counter); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment