I am seeing conflicting information about the maximum number of threads that a Rust program can spawn; some suggest arbitrary numbers like "32", sometimes a multiple of the number of cores the CPU has.
Each core can only run 1 thread at a time, i.e. hyperthreading is disabled. So, you can have a total maximum of 20 threads executing in parallel, one thread per CPU/core. That can mean 20 single-threaded jobs, 1 multi-threaded job with 20 threads, or anything in between.
Rust attempts to mitigate the negative effects of using threads, but programming in a multithreaded context still takes careful thought and requires a code structure that is different from that in programs running in a single thread.
Tokio has two kinds of threads: Worker threads. These run the tasks you spawn with tokio::spawn. Blocking threads.
We know that a process is a program in a running state. The Operating System maintains and manages multiple processes at once. These processes are run on independent parts and these independent parts are known as threads. Rust provides an implementation of 1:1 threading.
The threads provided by the Rust standard library are "OS threads", that is, they use the facilities of your operating system.
Therefore, a Rust program has no limit imposed by Rust itself, but rather, this limit would result from whatever your OS lets you do. You'd have to know the OS to know the true answer, which would be different for a given OS. For example, see this question: "Maximum number of threads per process in Linux?"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With