The while loop and the CPU.  
Author Message
David S. Anderson





PostPosted: Visual C++ General, The while loop and the CPU. Top

Is there a way I can use a normal while loop and make it so it does not take up 99% of the central processing unit There must be a way, because a loop is the only way to listen for requests using a Socket or TcpListener so there must be a way to only let it use a portion of the CPU.



Visual C++7  
 
 
CJPO





PostPosted: Visual C++ General, The while loop and the CPU. Top

The simple answer is: put a Sleep(1); in the loop. That would give you about a 1 millisecond response time.

This works because the loop and the function call to Sleep(1) take under a microsecond and so your program is actually sleeping a 1000 time longer then it is processing.

The better answer is probably use the blocking method like:

Socket^ socket = tcpListener->AcceptSocket();

If you need to do other work at the same time you can just use more then one thread. One blocks while the other does whatever work you need done.

The advantage of that is the AcceptSocket() thread can respond faster (as soon as the request comes in and get CPU time, which will happen right away if the worker thread is sleeping). On the other hand with an average response time of less then a milisecond the first method works fine for most cases and is easier to code.

The worker thread can go at its own pace.



 
 
Dav? S. A???????





PostPosted: Visual C++ General, The while loop and the CPU. Top

The Sleep function got rid of the CPU usage! Thanks.


 
 
Simple Samples





PostPosted: Visual C++ General, The while loop and the CPU. Top

a loop is the only way to listen for requests using a Socket or TcpListener
I think you are assuming something that is not true.

Sleep() is not the only solution and is not even the most common.

Please see The scope of this forum. There is a Microsoft newsgroup for network programming in which you will get better help. There are abundant tutorials and sample code you can easily find by seraching the internet. Web sites such as CodeProject, CodeGuru and many others can be very useful for this. I have a list of some in my Other Network Programming Sites and there are many others.



 
 
Dav? S. A???????





PostPosted: Visual C++ General, The while loop and the CPU. Top

I've read the networking newsgroups and nothing was of any help to me. And I know the sleep() method is not the only way to prevent the CPU usage, but using a loop to listen for incoming requests, I'm pretty sure that could be the only way, I don't see how else you could constantly listen for incoming requests on the socket.


 
 
Simple Samples





PostPosted: Visual C++ General, The while loop and the CPU. Top

I've read the networking newsgroups and nothing was of any help to me. And I know the sleep() method is not the only way to prevent the CPU usage, but using a loop to listen for incoming requests, I'm pretty sure that could be the only way, I don't see how else you could constantly listen for incoming requests on the socket.
It is definitely not the only way!



 
 
Dav? S. A???????





PostPosted: Visual C++ General, The while loop and the CPU. Top

Then could you provide some other methods that are valid


 
 
Simple Samples





PostPosted: Visual C++ General, The while loop and the CPU. Top

Network programming is outside the scope of the VC forums. If network programming were valid for these forums then I would say more, except since the subject is covered as much as it is elsewhere, it is foolish to spend time saying much about it. There ae already ample great resources about network programming.

There are important fundamental concepts that are common to many things, not just network programming. Concepts such as multithreading, asynchronous i/o and events. Events are critical to a UI system such as Windows and the ones used by Linux/Unix. Sleep is a very simple way for a program to yield for otehr processing, but it is so simple that it does not allow other events to be processed for a message loop. The Message Loop is central to the way that a Windows program works and understanding it will help to understand why Sleep is not the best solution. Yes for a Windows program a Message Loop is just a relatively simple while loop but Sleep is not used in a Message Loop.

Even if you were to write a console program without a Message Loop, network programming ususally uses events and threads to do I/O. There are abundant resources explaining that and much more.

You don't need us to help you with that. I am volunteering my time and you already have had the benefit of my time. I am more interested in helping people that really need help in the sense that the questions they ask are not easily answered. For the question of how to do network programming, there are plenty of places that answer that question that you can easily find, but it would require too much of my time to provide the same answers that you can easily find.