Jump to content

  • Log In with Google      Sign In   
  • Create Account


High CPU usage in basic C++ program


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 dAND3h   Members   -  Reputation: 214

Like
0Likes
Like

Posted 19 December 2012 - 03:09 PM

Hi, I am using ThreadLib from Ron Pentons MUD programming example. I have 2 threads in my program, one which recieves messages and one which sends messages. I create both of these threads(each has an infinite loop) and I also make sure to yieldThread() in my main loop. I end up with 50%cpu usage, isn't this a bit high? It does only run at around 13% on a better machine, but shouldn't it be closer to 0 than that?

Sponsor:

#2 hplus0603   Moderators   -  Reputation: 5186

Like
0Likes
Like

Posted 19 December 2012 - 08:32 PM

"yield thread" only yields until there is no other thread that wants the CPU, then you get the CPU back again.

You need to use a blocking primitive, like select() or sleep(), to actually reduce CPU usage. I recommend select(). Also, multiple threads is not recommended in the beginning of network programming. And, in fact, is often not needed at all.
enum Bool { True, False, FileNotFound };

#3 crancran   Members   -  Reputation: 406

Like
0Likes
Like

Posted 20 December 2012 - 12:40 PM

To add to what hplus0603 said, if you put your network connection into non-blocking mode, you can use select() and poll for data each loop and handle it accordingly. In a well structured design, networking really should be abstracted away to the point that if you decide to move networking to a separate thread later on, the impact to the simulation would be very minimal and non-invasive to change.

But if you want to use a separate thread, you can use asynchronous/non-blocking socket IO to handle reads/writes in a single thread outside your logic thread. This way the problem manifests itself to a simple problem of synchronizing a queue of messages between the logic/network threads for sending/receiving. But in most cases, this isn't really necessary and by avoiding it reduces the complexity of your game logic iterations.

Edited by crancran, 20 December 2012 - 12:42 PM.


#4 hplus0603   Moderators   -  Reputation: 5186

Like
0Likes
Like

Posted 21 December 2012 - 12:26 PM

if you put your network connection into non-blocking mode, you can use select() and poll for data each loop and handle it accordingly

 

You can do that even with blocking sockets. That's the whole point of select(): When select() says a file descriptor is "ready" for reading or writing, it GUARANTEES that the next call to recv() or send() will not block, even if the socket is "blocking."


enum Bool { True, False, FileNotFound };

#5 wood_brian   Banned   -  Reputation: 197

Like
0Likes
Like

Posted 24 December 2012 - 05:15 PM

"yield thread" only yields until there is no other thread that wants the CPU, then you get the CPU back again.

You need to use a blocking primitive, like select() or sleep(), to actually reduce CPU usage. I recommend select(). Also, multiple threads is not recommended in the beginning of network programming. And, in fact, is often not needed at all.

 

I switched from using select to poll in a couple of programs a few months ago.  The interface of poll is easier to work with.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS