Skip to content

Instantly share code, notes, and snippets.

@ProducerMatt
Last active May 29, 2022 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ProducerMatt/68655538ba54c1d1fc41ce3a5560f4fd to your computer and use it in GitHub Desktop.
Save ProducerMatt/68655538ba54c1d1fc41ce3a5560f4fd to your computer and use it in GitHub Desktop.
Scripts in various languages to create a massive number of threads. Abandon all hope ye who enter here
// Threads: 501
// real: 2293668 KiB
// 10k threads would be 43.66 GiB
// virt: 106113460 KiB
// 10k threads would be 1.973 TiB
const { Worker, isMainThread } = require('worker_threads');
function freeze(time) { //https://stackoverflow.com/a/46937705
const stop = new Date().getTime() + time;
while(new Date().getTime() < stop);
}
if (isMainThread) {
n = 501;
lasttime = Date.now();
for (i = 0; i<n; i++) {
new Worker(__filename);
if (i % 100 === 0) {
console.log(i, "threads");
newtime = Date.now();
console.log(newtime - lasttime, "millis since last checkin");
lasttime = Date.now();
}
}
console.log("DONE!");
} else {
// this block isn't even encountered for 15+ seconds after DONE!
freeze(5000);
}
# Threads: 500
# real: 13428 KiB
# 10k threads would be 262.3 MiB
# virt: 6145372 KiB
# 10k threads would be 117 GiB
import threading
import time
def Worker(arg):
while True:
time.sleep(5)
if __name__ == '__main__':
n = 10000
threads = []
for i in range(n):
thread = threading.Thread(target=Worker, args=(None,))
thread.start()
threads.append(thread)
print("DONE")
for i in range(n):
threads[i].join()
// threads: 500
// real: 6028 MiB
// 10k threads would be 120.560 MiB
// virt: 3066976 MiB
// 10k threads would be 58.5 GiB
use std::thread;
fn f() {
thread::sleep(
std::time::Duration::new(5, 0));
}
fn main() {
const target: i32 = 500;
// https://www.programming-idioms.org/idiom/56/launch-1000-parallel-tasks-and-wait-for-completion
let threads: Vec<_> = (0..target).map(|i| thread::spawn(move || f())).collect();
println!("Done");
for t in threads {
t.join();
}
}
;; Threads: 500
;; real: 14880 KiB
;; 10k threads would be 290.6 MiB
;; virt: 4555784 KiB
;; 10k threads would be 87.069 GiB
(use-modules (ice-9 threads))
(define GlobalThreadList '())
(define (AcquireThreads n)
(if (> n (length GlobalThreadList))
(begin (set! GlobalThreadList
(append GlobalThreadList
(list (call-with-new-thread
(lambda () (sleep 5))))))
(AcquireThreads n))
(display "Done")))
(define (BegoneThreads)
(map join-thread GlobalThreadList)
(set! GlobalThreadList '()))
(AcquireThreads 500)
(BegoneThreads)
// threads: 500
// real: 3940 KiB
// 10k threads would be 76.95 MiB
// virt: 8196200 KiB
// 10k threads would be 156.3 GiB
const std = @import("std");
pub fn StarCitizen() !void {
std.os.nanosleep(5, 0);
}
pub fn main() !void {
const stdout = std.io.getStdOut().writer();
const target: usize = 500;
var i: usize = 0;
var threads = std.ArrayList(std.Thread).init(std.testing.allocator);
while (i < target) : (i += 1) {
try threads.append(try std.Thread.spawn(.{}, StarCitizen, .{}));
}
try stdout.writeAll("Done!\n");
i -= 1;
while (i > 0) : (i -= 1) {
threads.items[i].join();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment