Guys, I found out that one of my Mono projects on Mac is having 100% CPU usage without any real load. Funniest thing that under Wine on the same machine it didn't have that problem.
I dig a lot and found that Mono's System.Threading.Thread.Sleep() resides on clock_nanosleep on non-windows platforms. That is generally good on Linux, but not on FreeBSD, Solaris and Mac OS X that don't have native clock_nanosleep at all and some substitution is performed instead. Thread.Sleep() is not a high precision timer, it does not need nanosecond resolution and can be easily fixed by using usleep, clock_sleep or even with mach_wait_until. Any of those lowers CPU usage in my case to 0-3% without any precision loss.
Even more, lot's of games are using Mono, MonoGame or Unity these days and games usually have loops with Thread.Sleep(1) inside. Those games on Mac OS X are heating the CPUs without any purpose meaning that millions of computers around the world right now uses excess power just because of clock_nanosleep!
So let's lower worldwide CO emission and excessive power usage by just fixing that one function!
I've not been able to reproduce this using the trivial test attached below. Could you provide a example that shows the 100% CPU behavior you have seen?
Created attachment 17595 [details]
Created attachment 17596 [details]
Thread.Sleep loop sample
Single Thread.Sleep call won't give you any significant CPU usage, you have to call it in a loop. It's common practice in games, multithreaded apps, network servers, etc.
Reopening this one. It's critical for infinite loops with Tread.Sleep(1)