Limit Threads in Rust

Is there a way to limit threads in Rust? I need to write a program that allows parallelism up to 30 threads, but honestly I have no idea how to that.

If you use something like a threadpool or scoped threadpool you can control the number of threads that will be used.

There are also other threadpool implementations that are probably also good but I haven't used them.

First off: You start the threads, so why don't you keep track of them with a AtomicUsize (or similar) and delaying starting new threads until the number is below 30 again?

Second: do you know rayon? There you can use the RAYON_NUM_THREADS environment variable to limit the number of threads.

1 Like

@newbie101, @hellow thanks for the suggestions. I will try them.
@hellow I am still a newbie in Rust and haven't figured everything out.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.