How does Rust ensure threads run on different cores? Idle cores etc?


#1

Tried doing a bit of googling but couldn’t find a definitive answer for this.

Don’t threads usually share the same core as their parent process? if not how does Rust achieve this?

They’re just plain OS threads from, what I understand


#2

There is no cross-platform API to do this currently. You can use platform-specific API to achieve this:


#3

Yes, they are, and it is entirely up to the operating system on how it handles that.

Thread execution is usually spread over multiple CPUs and not bound to the parent process.

Rust itself does nothing there to control how the operating does things.

The term you are probably searching for is “Processor Affinity” and described here: https://en.wikipedia.org/wiki/Processor_affinity. The article also explains well how this is not a programming language concern.


#4

It is up to the OS to distribute and schedule threads to enable them to execute as fast as possible. Most of the time, the OS does a great job here, so you don’t need to worry about it. In some rare cases, however, it might be desirable to override the decisions the OS scheduler makes, and that’s what those “set affinity” APIs are for.

Again, if you’re not sure, better don’t do this.