SFML community forums

Help => System => Topic started by: XeRaCKeR on July 08, 2012, 08:48:13 pm

Title: [SFML 1.6] MultiThreading on a Client Class getting error [Solved on SFML 2.0]
Post by: XeRaCKeR on July 08, 2012, 08:48:13 pm
Hi, I'm working on a Client App and I wish to create a Client class where I create 2 threads (R and S), who execute functions Receiver and Sender, respectively...
class Client {
    sf::IPAddress ServerAddress;
    sf::SocketTCP Socket;
    sf::Packet Packet;
    sf::Thread R, S;
    void Receiver(void*);
    void Sender(void*);
    bool connected;
    bool updated;
public:
    Client();
    sf::Packet Receive();
    void Send(sf::Packet);
    bool IsConnected();
    bool IsUpToDate();
};
 

Then, I get:
Thread.hpp|92|error: 'sf::Thread::Thread()' is protected|

I tried with
class Client : public sf::Thread {
and
class Client : private sf::Thread {
but this doesn't resolve it

How can I resolve it?

PD: Sorry if bad english ^^
Title: Re: [SFML 1.6] MultiThreading on a Client Class getting error
Post by: Laurent on July 08, 2012, 08:52:16 pm
There's a tutorial for sf::Thread. Have you read it?
Title: Re: [SFML 1.6] MultiThreading on a Client Class getting error
Post by: XeRaCKeR on July 09, 2012, 01:14:37 am
Yes, I readed it and I know how to create a Thread and launch it. I can use Thread with a function; and I can use Thread as class... but now, I wish use Thread with fuction on my class Client. Is it possible?

In brief, I need a class with 2 differentes threads, but when I declare sf::Thread on a class, I get:
Thread.hpp|92|error: 'sf::Thread::Thread()' is protected|
Title: Re: [SFML 1.6] MultiThreading on a Client Class getting error
Post by: Laurent on July 09, 2012, 07:56:20 am
Quote
but now, I wish use Thread with fuction on my class Client. Is it possible?
No, a member functions is not a non-member function. They are two totally different things.

Quote
In brief, I need a class with 2 differentes threads
This is impossible in SFML 1.6. You should switch to 2.0.
Title: Re: [SFML 1.6] MultiThreading on a Client Class getting error
Post by: XeRaCKeR on July 09, 2012, 01:21:33 pm
Ok, I am on SFML 2.0 now.

And now I get this error:
error: no matching function for call to 'sf::Thread::Thread()'|
note: candidates are: sf::Thread::Thread(const sf::Thread&)|

I can't declare sf::Thread without the constructor, and I can't construct on my class Client... how I should do it?
Title: Re: [SFML 1.6] MultiThreading on a Client Class getting error
Post by: Laurent on July 09, 2012, 01:41:30 pm
Why can't you construct your sf::Thread instance in the owner class constructor?
Title: Re: [SFML 1.6] MultiThreading on a Client Class getting error
Post by: XeRaCKeR on July 09, 2012, 01:48:26 pm
Like this?
Client::Client()
{
    sf::Thread R(&Receiver);
    cout << "Client running" << endl;
    ServerAddress ="**.**.**.**";
    Socket.connect(ServerAddress, *****);
    cout << "Connected" << endl;
}

I get the same error  :-\
Title: Re: [SFML 1.6] MultiThreading on a Client Class getting error
Post by: Laurent on July 09, 2012, 01:52:45 pm
No, you must use the initialization list (http://www.learncpp.com/cpp-tutorial/101-constructor-initialization-lists/).
Title: Re: [SFML 1.6] MultiThreading on a Client Class getting error
Post by: XeRaCKeR on July 09, 2012, 01:56:16 pm
Could you give me a example with sf::Thread please?  :-[
Title: Re: [SFML 1.6] MultiThreading on a Client Class getting error
Post by: Laurent on July 09, 2012, 02:02:52 pm
class Client
{
public:

    Client() : m_thread(&Client::R, this)
    {
    }

private:

    void R()
    {
        ...
    }

    sf::Thread m_thread;
}

I'll add an example in the tutorial, I just noticed that there's nothing about "using sf::Thread in a class".
Title: Re: [SFML 1.6] MultiThreading on a Client Class getting error
Post by: XeRaCKeR on July 09, 2012, 04:12:21 pm
It's works! :D Thanks you very much.

There is my code:

Client.hpp
#include <SFML/Network.hpp>
#include <iostream>
using namespace std;

class Client
{
    sf::IpAddress ServerAddress;
    sf::TcpSocket Socket;
    sf::Packet Packet;
    void Receiver();
    void Sender();
    bool connected;
    bool updated;
public:
    Client();
    sf::Thread R, S;
    sf::Packet Receive();
    void Send(sf::Packet);
    bool IsConnected();
    bool IsUpToDate();
};

Client.cpp
#include "Client.hpp"

Client::Client() : R(&Client::Receiver, this), S(&Client::Sender, this)
{
    cout << "Client is running!" << endl;
    ServerAddress ="**.**.**.**";
    Socket.connect(ServerAddress, *****);
    cout << "Client connected!" << endl;
    R.launch();
    S.launch();
}

void Client::Receiver() //Thread
{
    cout << "Receiver here!\n";
    while(1)
    {
        sf::Packet buffer;
        Socket.receive(buffer);
        cout << "Received!\n";
        Packet = buffer;
        buffer.clear();
        updated=false;
    }
}

void Client::Sender() //Useless Thread Yet (xD)
{
    cout << "Sender here!\n";
}

sf::Packet Client::Receive()
{
    updated=true;
    return Packet;
}

void Client::Send(sf::Packet Pack)
{
    connected = (Socket.send(Pack) == sf::Socket::Done);
}

bool Client::IsUpToDate()
{
    return updated;
}

bool Client::IsConnected()
{
    return connected;
}