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_Recv
referenceThat means if there's nothing to receive then
SDLNet_UDP_Recv
will return immediately with0
and your loop will iterate and callSDLNet_UDP_Recv
again which returns0
and 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