I've read that rust provides thread::available_parallelism() to know how many concurrent tasks you can execute. But does it work the same way for threadpool? Or can I set the limit to like 20 threads while the available parallelism is 8 for example?
Thanks!
You can set a higher number if you want -- this is "oversubscribing". You'll have more independent threads of execution, but the operating system scheduler will have to bounce them around more frequently, since there aren't enough actual CPUs for each.
In general, your system will be running other programs as well that are also competing for those CPUs, but the hope is usually that your program with a threadpool is the main busy thing at the time, which is why you'd try to match that to available_parallelism.
It's common to spin up more or less than the CPU count depending on the workload: if you have a lot of blocking tasks then having more threads (for example 2x the CPU count) means there's more likely to be work available for every CPU to do, or you can use less (commonly capping the CPU count) if you think you're not going to be able to load them all up and you want to reduce memory usage, or you want to avoid starving other threads or processes.
There's generally no straightforward way to know what the optimum value is so it's a good idea to make it configurable if that's appropriate for your users.