I am attempting to allocate a large number of threads on an Ubuntu 22.04 system, but I am running into issues with spawning threads. I included the full error below, but the core of it seems to be failed to set up alternative stack guard page: Cannot allocate memory (os error 12). This is somewhat annoying since there should be plenty of address space on this x64 machine.
To try to diagnose the problem, I created this example which simply creates a ton of threads then exits. The error above was created from this code.
use std::thread::spawn;
use std::sync::Barrier;
use std::sync::Arc;
pub fn main() {
let num_threads = 20000; // Works up to about 16k threads
let barrier = Arc::new(Barrier::new(num_threads));
let mut threads = Vec::new();
for _ in 0..num_threads {
let barrier_clone = barrier.clone();
threads.push(spawn(move || {
barrier_clone.wait();
}));
}
for handle in threads {
handle.join().unwrap();
}
println!("No issues!");
}
I ran this on an Ubuntu 22.04 r6g.medium AWS EC2 instance (1 vCPU, 8 GiB Memory).