For example i have a class player and some sub-Class Player1, Player2, extends Player using C++. Class Player have a method sendMessage() and receive Message() , and all of Player1,2 will override "run" to send message between each other !
class Player {
public:
    virtual void sendMessage();
    virtual void receiveMessage();
}
class Player1: public Player {
public:
    void sendMessage();
    void receiveMessage();
}
This question already has answers here: Start thread with member function (5 answers) Closed 8 years ago. I have a Class Player, and some sub-Class Player1, Player2, Player3 extends Player using C++. Class Player have a method "run", and all of Player1,2,3 will override "run" to do different things.
class Player {
public:
    virtual void run();
}
class Player1: public Player {
public:
    void run();
}
In "main" function I will create some instances of Player1,2. and i need to run every instance in a thread :
int main() {
    Player1 player1;
    Player2 player2;
    thread thread1(&Player::sendMessage,player1);
    thread thread2(&Player::receiveMessage,player1);
    thread thread3(&Player::sendMessage, player2);
    thread thread4(&Player::receiveMessage,player2);
    thread1.join();
    thread2.join();
    thread3.join();
    thread4.join();
    return 0;
}
In this way I am running each method of object player in thread, but how can I run all the methods of an object in one single thread? I am asking about this because I want the two objects to send messages to each other in a synchronized way!
Aucun commentaire:
Enregistrer un commentaire