🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Winsock Processor useage.

Started by
18 comments, last by griffenjam 22 years, 5 months ago
I''ve noticed that when I run my server my processor useage jumps to 100%. This is just while the server is waiting for a connection. Is there anything I can do about this? Jason Mickela ICQ : 873518 E-Mail: jmickela@sbcglobal.net
Please excuse my spelling
-"I''m Cloister the Stupid" Lister (Red Dwarf)
"The paths of glory lead but to the grave." - Thomas GrayMy Stupid BlogMy Online Photo Gallery
Advertisement
Yes, make sure you are using blocking sockets or I/O completion callbacks with a Waitxxx/SleepEx function.

If your CPU usage is at 100%, you must be in a tight loop like a while(1), for(; etc without ever calling a blocking Winsock call.

Dire Wolf
www.digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
Thats what I thought too, but I am using blocking sockets.
The server only has two threads while it is running without any connections. The main thread, and the listening thread. The listening thread has a blocking call in it, and the main thread is a generic windows message pump.

Jason Mickela
ICQ : 873518
E-Mail: jmickela@sbcglobal.net


Please excuse my spelling


-"I''m Cloister the Stupid" Lister (Red Dwarf)
"The paths of glory lead but to the grave." - Thomas GrayMy Stupid BlogMy Online Photo Gallery
One of the blocking calls may be failing and returning an error right away - 1,000,000''s of times per second
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
You wouldn''t happen to be using PeekMessage instead of GetMessage in the message pump would you?

Dire Wolf
www.digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
Yes, I am.
I had though about that, but I don''t understand why that would make a difference. There isn''t anything in the else part of the if statement. Also if I comment out the servers startup function, the CPU load never increases.

Jason Mickela
ICQ : 873518
E-Mail: jmickela@sbcglobal.net


Please excuse my spelling


-"I''m Cloister the Stupid" Lister (Red Dwarf)
"The paths of glory lead but to the grave." - Thomas GrayMy Stupid BlogMy Online Photo Gallery
PeekMessage doesn''t block, GetMessage does. That''s the problem, you''re spinning your wheels waiting for messages. If you have no other logic in your loop, i.e:

  while(true){  if(PeekMessage(...)){    //HANDLE MESSAGE  }}  


Then there''s no point in using PeekMessage() vs. GetMessage(). Normally you''d want to use PeekMessage() if you have some processing you need to do every frame.
At the moment there isn''t anything there, there will be later though.

Jason Mickela
ICQ : 873518
E-Mail: jmickela@sbcglobal.net


Please excuse my spelling


-"I''m Cloister the Stupid" Lister (Red Dwarf)
"The paths of glory lead but to the grave." - Thomas GrayMy Stupid BlogMy Online Photo Gallery
  while(true){ if(PeekMessage(...)){  //PROCESS MESSAGE }else{  //DO NON-MESSAGE STUFF  Sleep(1); }}  


Try that. If you Sleep(0) and there''s no other active thread that needs CPU time, your thread will continue to run (which isn''t a bad thing). With a (1) there you''re garanteed to give another thread a timeslice.
Actually, what JonStelly said is not quite true. As far as I know, reading the documents and through experience, Sleep(0) will work for you because even though you are requesting a sleep time of 0 milliseconds, the function call causes a context switch to be performed, and all the other threads of equal priority will be executed in a round-robin fashion before your program gets a return on the Sleep() call.

The differences between Sleep(0) and Sleep(1) should be almost inperceptable.

This topic is closed to new replies.

Advertisement