Skip to main content
gamedev.net
🔒 Locked

Windows weird problem

Started by Shadowed 2 replies 650+ views
Original Post
Shadowed
Shadowed
Programming one project, I came to very strange behavior of Windows when writing a while(true) loop. Consider the following winpseudocode:

WinMain()
{
CreateFirstThread();
CreateSecondThread();

while(true);
}

FirstThread()
{
while(true);
}

SecondThread()
{
while(true);
}

Yes, as far as I can understand the written above, nothing special is written, the code should execute perfectly well [not taking into consideration the meaninglessness of the code]. And the good part is - it does execute perfectly, on Windows 2000 Professional. But blah, choose Windows 2000 Advanced Server and there you go - 99% CPU time consumed on the program immediately after the execution, and after a while precious Windows dies. I thought that might be because of the threads, and brought the source to a state looking like:

WinMain()
{
while(true);
}

But uh, this didn''t change a thing. I guess the problem is clear, and the big thing is that I can''t go along with Windows 2000 Professional, at least Windows 2000 Server is mandatory. So before I uninstall Advanced Server and try with Server, maybe you can help me a bit.. Thanks a lot. -- I hate you.
--I hate you.
Endurion
Endurion
Do NEVER do a wait loop like that.

A) There''s no stop condition.

B) It will eat up your CPU. Always use a Sleep there if you want to keep other threads running and just waiting. Sleep is the nice way of telling the os that other threads and processes may do their work now.
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>
Shadowed
Shadowed
quote:
Original post by Endurion
Do NEVER do a wait loop like that.

A) There''s no stop condition.

B) It will eat up your CPU. Always use a Sleep there if you want to keep other threads running and just waiting. Sleep is the nice way of telling the os that other threads and processes may do their work now.




A) that was a pseudocode, in the actual source code it is handled
B) It shouldn''t eat my CPU. sleep() is something I can''t afford on a high-performance server, I''m sorry. Also, if you''re right [though you''re not], why does the program execute perfectly on Windows 2000 Professional.

--
I hate you.
--I hate you.
Endurion
Endurion
while ( true ) indicates that the curren thread doesn''t have anything to do. Why not call Sleep( 0 ) to give others a chance.
That wouldn''t hurt much.

I''ve noticed that waiting for another thread with a while ( condition) without sleep would work fine on Win95/98 but not on NT++. Your thread will check the condition all the time.

There seem to be some differences on how windows handles the foreground thread vs. other threads.
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.