Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Tcp socket in sf::Thread CPU usage  (Read 2781 times)

0 Members and 1 Guest are viewing this topic.

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Tcp socket in sf::Thread CPU usage
« on: August 27, 2018, 02:56:41 pm »
Posting in General since it concerns both Network and System.

I'm running a blocking socket (tcp) in an sf::Thread. The program uses 1 percent CPU when I don't run the thread and 30 percent CPU when I do run it. VS Diagnostics show that the function that runs in the thread uses all that extra CPU (see the code for the function). Is this normal or am I doing this wrong?

        while (1)
        {
                sf::Packet packet;

                sf::Int32 code;

                if (Socket.receive(packet) == sf::Socket::Done)
                {
                        packet >> code;
                        PacketCodes.push_back(code);

                        switch (code)
                        {
                                // handle stuff
                        }
                }
        }
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Tcp socket in sf::Thread CPU usage
« Reply #1 on: August 27, 2018, 03:41:15 pm »
What function and which thread is take all the CPU time?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: Tcp socket in sf::Thread CPU usage
« Reply #2 on: August 27, 2018, 06:04:04 pm »
What function and which thread is take all the CPU time?

Well, the program consists of the main thread and one extra thread in which I run the code I posted above.

This extra thread is using almost all of the CPU. More specifically, the CPU usage seems to be divided evenly by:

1 - sf::TcpSocket::receive
2 - sf::Packet::~Packet
3 - sf::Packet::Packet

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Tcp socket in sf::Thread CPU usage
« Reply #3 on: August 27, 2018, 06:33:33 pm »
So is it truly blocking? Are you constantly receiving data?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: Tcp socket in sf::Thread CPU usage
« Reply #4 on: August 27, 2018, 06:56:17 pm »
So is it truly blocking? Are you constantly receiving data?

OK, I remembered that the server I wrote sends the packets it receives to every other client.

        for (unsigned int i = 0; i < Clients.size(); i++)
        {
                if (Selector.isReady(*Clients[i]))
                {
                        sf::Packet packet;

                        if (Clients[i]->receive(packet) == sf::Socket::Done)
                        {
                                for (unsigned int j = 0; j < Clients.size(); j++)
                                {
                                        if (i != j) { Clients[j]->send(packet); }
                                }
                        }
                }
        }
 

... and upon connecting another client to the server, both clients were using only 1 percent CPU as expected.

So, I guess they have to be constantly receiving data or sf::TcpSocket::receive goes nuts?

Sorry for being a noob, still very new to this network voodoo.

 

anything