Avoid locking on local server shutdown

This commit is contained in:
Fabio Bas 2016-07-19 11:32:00 +02:00
parent d82564d84c
commit d61d9c98a0
3 changed files with 28 additions and 22 deletions

View file

@ -181,6 +181,30 @@ Servatrice::Servatrice(QObject *parent)
Servatrice::~Servatrice()
{
gameServer->close();
// clients live in other threads, we need to lock them
clientsLock.lockForRead();
for (int i = 0; i < clients.size(); ++i)
QMetaObject::invokeMethod(clients.at(i), "prepareDestroy", Qt::QueuedConnection);
clientsLock.unlock();
// client destruction is asynchronous, wait for all clients to be gone
bool done = false;
class SleeperThread : public QThread
{
public:
static void msleep(unsigned long msecs) { QThread::usleep(msecs); }
};
do {
SleeperThread::msleep(10);
clientsLock.lockForRead();
if (clients.isEmpty())
done = true;
clientsLock.unlock();
} while (!done);
prepareDestroy();
}