Home > Archive > Unix Programming > March 2006 > 100% CPU
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| calcop@gmail.com 2006-03-30, 4:00 am |
| C++ Question:
Hello everyone,
Could someone please point me in the right direction? Here is my
problem. I have created a telnet server for a records management
system. To handle multi users, I made this server thread out each
connection. After threading the connection it threads out to handle the
user interaction. The connection thread is constantly reading data from
the socket and putting it into a variable contained into a class called
socketBuffer.
I have two functions to read data. They are getChar() and getString(int
stringLength). The getChar() function, for example, loops until the
length of socketBuffer is one. It then returns character received
though the socket connection. The getString function does the same
thing, but returns the value when the length equals the specified
length.
Anyway, when I call getChar and getString, the CPU peaks at 100% ? Is
there any better way to do this? Or any other way to wait until I
receive the data?
Please help, if you can.
| |
| Nils O. Selåsdal 2006-03-30, 7:02 pm |
| calcop@gmail.com wrote:
> C++ Question:
>
> Hello everyone,
> Could someone please point me in the right direction? Here is my
> problem. I have created a telnet server for a records management
> system. To handle multi users, I made this server thread out each
> connection. After threading the connection it threads out to handle the
>
> user interaction. The connection thread is constantly reading data from
>
> the socket and putting it into a variable contained into a class called
>
> socketBuffer.
>
> I have two functions to read data. They are getChar() and getString(int
>
> stringLength). The getChar() function, for example, loops until the
> length of socketBuffer is one. It then returns character received
> though the socket connection. The getString function does the same
> thing, but returns the value when the length equals the specified
> length.
>
> Anyway, when I call getChar and getString, the CPU peaks at 100% ? Is
> there any better way to do this? Or any other way to wait until I
> receive the data?
Busy looping is usually a bad thing. Think about locking too.
If you're using pthreads, you can use pthread condition variables
to signal another thread something interresting has happend, and
allow it to resume operation.
| |
| calcop@gmail.com 2006-03-30, 7:02 pm |
| Thanks, I will check it out.
|
|
|
|
|