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

Author Topic: Crash on selector.wait() with a std::thread  (Read 3777 times)

0 Members and 1 Guest are viewing this topic.

Naralas

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Crash on selector.wait() with a std::thread
« on: March 03, 2015, 02:34:51 pm »
Hello,

I'm learning to use the threads with the SFML network.
I have a thread (std) for the server on which it listens all the time but I always get the error segementation default on the selector.wait.

My class in which I initialize my thread :

void Application::Start()
{
    Network net1(this->port);
    std::thread server_thread(&Network::Run,&net1);
    server_thread.detach();
}

The run fonction of the network class for the server :

void Network::Run()
{
    listener.listen(this->port);
    while(true)
    {
        if (selector.wait())                          // crash here
        {
            // Test the listener
            if (selector.isReady(listener))
            {
                cout << "Client connected." << endl;
                ...
            }
        }
     }
}

Thanks for your help.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Crash on selector.wait() with a std::thread
« Reply #1 on: March 03, 2015, 02:46:18 pm »
Unless you provide more information on what goes wrong... just guessing here.

void Application::Start()
{
    Network net1(this->port);
    std::thread server_thread(&Network::Run,&net1);
    server_thread.detach();
} // net1 destructed
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Naralas

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Crash on selector.wait() with a std::thread
« Reply #2 on: March 03, 2015, 03:16:42 pm »
Thanks it was this. I don't know how I couldn't see that.
I found another problem. The clients seems to connect and send data to the server but the server does not receive anything.

Naralas

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Crash on selector.wait() with a std::thread
« Reply #3 on: March 09, 2015, 02:57:36 pm »
Sorry I wanted to post the full code before, but I had a lot of things to do this week.

So here's the code of the client :
int port = 48000;
sf::TcpSocket socket;
sf::Socket::Status status = socket.connect("127.0.0.1",port);

if(status != sf::Socket::Done)
        std::cout << "Unable to connect on port " << port << std::endl;
else
{
        std::cout << "Connected to the server on port " << port << std::endl;
        sf::Packet pack;
        std::cin.get();
        pack << "aaaaaa";

        if (socket.send(pack) != sf::Socket::Done)
            std::cout << "Unable to send the packet." << std::endl;
        else
            std::cout << "Packet send." << std::endl;

}

And here is the full code of the server (in the thread) :
listener.listen(this->port);
cout << "started on the port " << port << endl;
while(true)
{
         if (selector.wait())
        {
            // Test the listener
            if (selector.isReady(listener))
            {
                cout << "Client connected." << endl;
                // The listener is ready: there is a pending connection
                sf::TcpSocket* client = new sf::TcpSocket;
                if (listener.accept(*client) == sf::Socket::Done)
                {
                    // Add the new client to the selector so that we will
                    // be notified when he sends something
                    selector.add(*client);
                    //emit New_Client(client);
                }
                else
                {
                    // Error, we won't get a new connection, delete the socket
                    delete client;
                }
            }
        }
        else
        {
            // The listener socket is not ready, test all other sockets (the clients)
            for (std::list<sf::TcpSocket*>::iterator it = clients.begin(); it != clients.end(); ++it)
            {
                sf::TcpSocket& client = **it;
                if (selector.isReady(client))
                {
                    // The client has sent some data, we can receive it
                    sf::Packet packet;
                    if (client.receive(packet) == sf::Socket::Done)
                    {
                        string data;
                        packet >> data;
                        cout << "Received : " << data << endl;
                    }
                }
            }
        }
    }

The clients tells me he's connected on the server and was able to send the packet but it's like I get no connection and no data on the server.
« Last Edit: March 09, 2015, 02:59:14 pm by Naralas »

Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
Re: Crash on selector.wait() with a std::thread
« Reply #4 on: March 09, 2015, 07:03:28 pm »
Personally, I don't use threads anymore, even for networking, this is the source of many bugs. :/

I had a similar problem as you with std::thread, my server didn't received any messages with the UDP protocol...

Since I don't use an std::thread it works fine. :)

And I don't have a lot of experience in multi-thread programing, so if you haven't a good experience with multi-threading programming, I don't recommand it.

PS : And where did you add the listener to your selector ? You have to add it before your loop if you want that it works.

selector.add(listener);
 
« Last Edit: March 09, 2015, 07:08:23 pm by Lolilolight »

Naralas

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Crash on selector.wait() with a std::thread
« Reply #5 on: March 10, 2015, 08:34:49 am »
Yeah I added it before in the code. Thanks for your help I guess i'll learn more about the C++ the threads and using the SFML before trying to do a correct multi threaded server.

Rosme

  • Full Member
  • ***
  • Posts: 169
  • Proud member of the shoe club
    • View Profile
    • Code-Concept
Re: Crash on selector.wait() with a std::thread
« Reply #6 on: March 10, 2015, 03:12:39 pm »
Multithreading has many pitfall and if not done correctly, will often lead to less performance. If you're still in a process of learning how to use them correctly, you might want to try something else than a network service, since it is also one of the crapiest thing to debug. Most of the time, you can do a very good job without multithreading.
GitHub
Code Concept
Twitter
Rosme on IRC/Discord