Re: MSDN volatile sample



A thread running in a tight loop with Sleep(0) will consume all remaining
CPU time, AND won't let lower priority background threads to run. It won't
starve other threads of the same priority, though.

"George" <George@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:4FB8F7B7-4452-435C-81AE-6267ED829E4C@xxxxxxxxxxxxxxxx
Thanks Alexander,


You mean Sleep(0) will consume 100% CPU time? I can not believe it because
in MSDN, it is mentioned that Sleep(0) will let current thread *If you
specify 0 milliseconds, the thread will relinquish the remainder of its
time
slice but remain ready*. I assume it means the current thread will
contribute
its remaining running time to other ready status thread.

http://msdn2.microsoft.com/en-us/library/ms686298(VS.85).aspx

Do you have a test program which proves Sleep(0) will still occupy 100%
CPU
time?


regards,
George

"Alexander Grigoriev" wrote:

Sleep(0) is causing kernel trip anyway (lots of them in the usual case,
since it's called in a loop), compared to only 2 for WFSO/SetEvent pair.
And
this loop doesn't allow to stop processor clock to reduce CPU power. When
I
see an application comsuming 100%, that gives me doubts about its general
quality, and I get rid of it.

"Alex Blekhman" <tkfx.REMOVE@xxxxxxxxx> wrote in message
news:uFdF0$WSIHA.3940@xxxxxxxxxxxxxxxxxxxxxxx
"Alexander Grigoriev" wrote:
Anyway, the sample illustrates very poor synchronization practice
which
should not be followed. Proper signalling using events makes volatile
qualifier unnecessary.

There is nothing wrong with this technique if it used correctly.
Synchronization with events or other kernel objects brings the high
price
of user space to kernel space trip. It is not uncommon that you need to
synchronize within single process only. In these cases volatile
variables
may provide adequate solution.

Alex






.



Relevant Pages

  • Re: Changing process priority
    ... How do I make sure a simple whileloop ... doesn't consume half my CPU? ... Though sometimes there is a "idle process" that runs ... an infinite loop consuming all leftover CPU time, ...
    (comp.lang.c)
  • Re: Continuous loop handling
    ... > monitor the hardware input in a while loop. ... Tough my gui is active by this ... Is there a way to free up the cpu time ... Sleep somewhere inside your loop. ...
    (microsoft.public.vc.mfc)
  • Re: How Do I Use An SSLServerSocket With ServerSocketChannel?
    ... it's Java 5.0 or give up. ... endless loops of reading a socket and writing to the other one while ... loop and when it does a ServerSocket.accept, it goes into the inner loop ... isn't that going to steal a lot of CPU time ...
    (comp.lang.java.help)
  • Re: Do loop memory consumption?
    ... I need to interrupt the loop when user presses the STOP button. ... I would like to be able to restrict its use of CPU time to minimum since ... Dim Start, Finish, TotalTime As Double ... Finish = Start + mSeconds ' Set end time for mseconds duration. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Serial Port CPU Usage Problem
    ... Before answering you I would like you to compile and execute test ... consumes a lot of CPU time on your machine? ... transaction does not solve the problem and even one call to (Serial Port) ... ReadFile/WriteFile in a cycle will consume the same CPU time. ...
    (microsoft.public.win32.programmer.kernel)