How to interrupt other std :: threads C ++

I have a server that is built in the thread behind the client. Recently, I have encountered a problem that it is very difficult for me to find a solution, so I thought that I would ask for help.

There is a lobby on my server, there are many rooms in the lobby (all of them belong to users), and there are players in the rooms. Each room has an administrator, when the administrator wants to leave - the room is closed, and all users must return to the lobby.

Now I already have a working code, but there is a problem, I have no idea how to get other clients to leave the room. The code that executes in the stream is as follows:

while(in_lobby)
{
//Receive a message
//Do stuff
//In certain cases change the Boolean to fit to the situation
//Send a comeback
}

while(in_room)
{
//Receive a message
//Do stuff
//In certain cases change the Boolean to fit to the situation
//Send a comeback
}

while(in_game)
{
//When a game started
//Not practical right now, though
}

( , , , , ).

, DOES , while . ? recv(), .

, , ( - , ), ( , - , , ).

:

, ? , ( , - , , ), ?

+4
4

" recv(), ."

, recv() .
select() Windows Socket API, .

, , , select(). CPU , , .

- (select() > 0), ,

FD_ISSET(s, *set)

.

+1

recv(), .

, , , :

  • , recv().
  • .

, recv() .

winsock, - . -, , . - .

, , , recv() , ; , , , , , .

+1

πάντα ῥε, Provider-Consumer.

, , . , , "Job" ( , ).

, , recv, .

:

Queue queue; // This has to be thread save.

class Provider: Thread
{
    void run(queue)
    {
      while(1)
      {
          message = recv();       // This is where waiting occurs.
          queue.push(message);
      }
    }   
}


// This looks like it fits inside another thread. ;)
while (some_condition)
{
    message = queue.pop(); // This returns immediately.
    if (message)
    {
      //... so some things.  
    }
}
0

Change your code so that there is one and only one place where you block recv. Then you can move the client around everything you need without breaking the thread blocked in recv. You still want to receive the message if the client sends it, right?

So, when the room is closed, the thread that closes the room can get clients out of it without breaking the threads waiting for messages from these clients.

0
source

All Articles