Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a polling loop in C# that needs to poll every 100 microseconds on average < EDIT> (given of course that there is no excessive preemptive thread context switch carried out by Windows due to core shortage) < /EDIT>.

As there is no time for a reschedule, Sleep(1) will not do.

So I decided to dedicate a thread (and in practise, a core when setting affinity) and use a Thread.SpinWait for a set of cycles for each iteration. While this works fine, it eats an unnecessary amount of power. The 100 microseconds would be plenty enough for the CPU to pause (while not enough to have the thread temporary removed from the scheduler as the Windows time-slice would be way to long).

Instead, I was thinking of using the Intel PAUSE instruction but I'm not sure it will trigger the Intel CPU to suspend the hardware thread. Intel claims it preserves power and should be used in a spin loop, but as the pause is as long a as 100 microsecond, I really want the core to go into a C1 mode sleep.

Any ideas?

Edit: I'm polling a third party API, so there is no synchronization event to block on.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Naturally using synchronization primitives and timers is a preferred way to go to avoid CPU/power-hungry busy wait. However, if you need to poll so frequently - there's no way to achieve this by conventional means, at least in user mode.

One simple thing that you may do is to include a pause CPU instruction within your loop. In MSVC it's implemented by an intrinsic YieldProcessor() method.

Beyond this - probably in kernel-mode programming only. There you may use a high-precision multimedia timer.

Edit:

About SetWaitableTimer. This may be an option. Unlike "traditional" Win32 waiting functions (such as Sleep, WaitForSingleObject and etc.) it uses a high-precision timeout as a parameter.

However user-mode timers are asynchronous in nature. Let's assume the timer becomes active with high precision (thpigh this is not obvious, "traditional" Win32 waiting functions are accurate up to tick quanta, order of tens of milliseconds). After the timer becomes active - it releases the appropriate waiting thread(s). But thread scheduler doesn't have to attach this thread to the execution immediately - it may wait for the next time slice. Or even delay the thread execution more if there are concurrent threads.

In conclusion: the idea seems worth trying. But I won't be surprised if this is more-or-less equivalent to using Sleep.

share|improve this answer

Seeing as your waits are lengthy, you might find SSE3 memory region monitors far more efficient in terms of power savings, however, its not really designed as a blocking spin-wait but as an alertable-wait, but may still prove a viable alternative.

Getting this in C# will require an external DLL however, that provides an interface to the C++ intrinsics (_mm_mwait and _mm_monitor).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.
{"cc_re__cc_re__cc_by_sa_3_0": ["                  <li><a href=\"//area51.stackexchange.com\" title=\"proposing new sites in the Stack Exchange network\">Area 51</a></li>\r\n                        <li><a href=\"//careers.stackoverflow.com\">Stack Overflow Careers</a></li>\r\n                    \r\n                </ol>\r\n            </td>\r\n    </tr>\r\n</table>\r\n                </div>\r\n            </div>\r\n\r\n            <div id=\"copyright\">\r\n                site design / logo &#169; 2015 stack exchange inc; user contributions licensed under <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" rel=\"license\">cc by-sa 3.0</a> \r\n                with <a href=\"http://blog.stackoverflow.com/2009/06/attribution-required/\" rel=\"license\">attribution required</a>\r\n            </div>\r\n            <div id=\"svnrev\">\r\n                rev 2015.6.3.2631\r\n            </div>\r\n            \r\n        </div>\r\n    </div>\r\n    <noscript>\r\n        <div id=\"noscript-warning\">Stack Overflow works best with JavaScript enabled<img src=\"http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif\" alt=\"\" class=\"dno\"></div>\r\n    </nosc"], "copyright__copyright__copyright": ["of power savings, however, its not really designed as a blocking spin-wait but as an alertable-wait, but may still prove a viable alternative.\n\nGetting this in C# will require an external DLL however, that provides an interface to the C++ intrinsics (_mm_mwait and _mm_monitor).\n\nshare|improve this answer\n\nYour Answer\n\n\u00a0\ndiscard\n\nBy posting your answer, you agree to the privacy policy and terms of service.\n\nNot the answer you're looking for? Browse other questions tagged or ask your own question."]}