(In the beginning, sorry for my english
)
I've got problem with threads. I'm making network game. Singleplayer mode is done, but multiplayer online mode is a bit laggy. This is my previous sending and receiving data function:
//////////////////////////////////////////////////////Server
void Engine::serverNetworkUpdate()
{
Lock lock(mutex);
float data;
while (game)
{
//sending data
data = otherData;
socket->send(sendingPacket);
sendingPacket.clear();
//receiving data
socket->receive(receivingPacket);
if (receivingPacket >> data)
{
otherData = data;
receivingPacket.clear();
}
}
}
//////////////////////////////////////////////////////Client
void Engine::clientNetworkUpdate()
{
Lock lock(mutex);
float data;
while (game)
{
//receiving data
socket->receive(receivingPacket);
if (receivingPacket >> data)
{
otherData = data;
receivingPacket.clear();
}
//sending data
data = otherData;
socket->send(sendingPacket);
sendingPacket.clear();
}
}
This function is launching by a thread. I deduced that it would be better to separate send and receive on two different threads. Program would not have to wait to receive data and in meantime would send other data. OK, just divide sending and receiving function and launch two threads. There are these functions:
//////////////////////////////////////////////////////Server
void Engine::serverSending()
{
Lock lock(mutex);
float data;
while (game)
{
data = otherData;
sendingPacket << data;
socket->send(sendingPacket);
sendingPacket.clear();
}
}
void Engine::serverReceiving()
{
Lock lock(mutex);
float data;
while (game)
{
//receiving data
socket->receive(receivingPacket);
if (receivingPacket >> data)
{
otherData = data;
receivingPacket.clear();
}
}
}
//////////////////////////////////////////////////////Client
void Engine::clientSending()
{
Lock lock(mutex);
float data;
while (game)
{
data = otherData;
sendingPacket << data;
socket->send(sendingPacket);
sendingPacket.clear();
}
}
void Engine::clientReceiving()
{
Lock lock(mutex);
float data;
while (game)
{
socket->receive(receivingPacket);
if (receivingPacket >> data)
{
otherData = data;
receivingPacket.clear();
}
}
}
I initiate these threads in constructor:
Engine::Engine(..., int whatMode)
{
//... other code
else if (whatMode == 10)
{
sendingThread = new Thread(&Engine::serverSending, this);
receivingThread = new Thread(&Engine::serverReceiving, this);
runGameOnlineServer();
}
else if (whatMode == 11)
{
sendingThread = new Thread(&Engine::clientSending, this);
receivingThread = new Thread(&Engine::clientReceiving, this);
runGameOnlineClient();
}
}
I'm launching these threads in runGameOnlineServer() and runGameOnlineClient() functions:
//////////////////////////////////////////////////////Server
void Engine::runGameOnlineServer()
{
receivingThread->launch();
sendingThread->launch();
while (game)
{
//events, updating, drawing etc.
}
}
//////////////////////////////////////////////////////Client
void Engine::runGameOnlineClient()
{
receivingThread->launch();
sendingThread->launch();
while (game)
{
//events, updating, drawing etc.
}
}
This is destructor:
Engine::~Engine()
{
//closing thread and socket, if game was online
if (gameMode == 10 || gameMode == 11)
{
sendingThread->terminate();
receivingThread->terminate();
delete sendingThread;
delete receivingThread;
socket->disconnect();
}
}
Simple in theory. But... My second thread doesn't launch. So if server first launch sendingThread, receivingThread doesn't launch. The same with client: client first launch receivingThread, so sendingThread doesn't launch. Server sends data (in this case left player and ball positions), but doesn't receive data (in this case right player position, it's Pong game). Client receives data, but doesn't send. To make sure I checked console output: only one thread was launched.
Does anyone know what's wrong? Why only one thread works?