Skip to content

Instantly share code, notes, and snippets.

@therustmonk
Created November 4, 2015 22:40
Show Gist options
  • Save therustmonk/548180c57c0919015354 to your computer and use it in GitHub Desktop.
Save therustmonk/548180c57c0919015354 to your computer and use it in GitHub Desktop.
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