.net - How is Thread.SpinWait useful on a single-core CPU? -
on single core machine, if running thread spin-waits because waiting release of lock thread holds, isn't spin-wait waste of cpu time because thread holds lock anyway on wait queue?
therefore, spin waiting release of lock valuable on machines multiple cores?
for matter, question applies slim
objects such semaphoreslim
, readerwriterlockslim
well.
yes, spin waiting useless , counterproductive on single-core machines -- such machines increasingly rare.
.net 4.0 introduces more advanced spinwait
structure. if used on single-core machine give current thread's time slice , allow progress made on other threads, since busy waiting length of time nothing hold processor. (spin waiting in sense still make sense hardware drivers on single core machines since hardware can make progress in background, hardware drivers aren't written in il, of course.)
similarly, *slim
types not employ spinning if used on single-core machine. still worthwhile use when possible because, unlike non-slim
counterparts, written faster, simpler , require less resources (semaphore
backed kernel object, example, semaphoreslim
not).
in general, should prefer *slim
types regardless of number of cores , use spinwait
structure if need spinning (almost never, unless you're implementing own synchronization primitive), since not spin unless makes sense so.