Running my listen function in a seperate thread seems to use up a lot of CPU Is it considered ok to use Delays to reduce cpu usage or am I using threads all wrong ?
// Running in a seperate Thread
void Server::listen()
{
while (m_running)
{
if (SDLNet_UDP_Recv(m_socket, m_packet) > 0)
{
//Handle Packet Function
}
}
}
From the
SDLNet_UDP_RecvreferenceThat means if there's nothing to receive then
SDLNet_UDP_Recvwill return immediately with0and your loop will iterate and callSDLNet_UDP_Recvagain which returns0and so on. This loop will never sleep of pause, so of course it will use as much CPU as it can.A possible solution is indeed to add some kind of delay or sleep in the loop.
I would suggest something like