Is it okay to park main thread instead of sleeping or yielding in a forever running script?

I was wondering if there's any downsides to parking the main thread instead of using yield_now or sleep?

Are you just trying to wait forever? If so, loop { thread::park(); } is a perfectly fine way to do it, as is loop { thread::sleep(a_long_time); }. You definitely shouldn’t use loop { thread::yield_now(); }, as that will busy loop and use 100% of the CPU.

2 Likes

I haven't noticed any. Well, other than the documented caveats...

A call to park does not guarantee that the thread will remain parked forever, and callers should be prepared for this possibility.

It may also return spuriously, without consuming the token.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.