Answer the question
In order to leave comments, you need to log in
Why can't I terminate the thread correctly?
Good day to all!
Problem such: I write the multithreaded network application on QT. The server has a given fixed number of threads. When a client connects, an object is created to work with that client and placed on the stream. When the client disconnects, the object emits a signal, at which the server should correctly stop this particular thread.
But at the moment of stopping and deleting the thread, it gives the following error:
QThread: Destroyed while thread is still running
Apparently, I don't understand how to properly stop a thread, and therefore I ask you to help!
Creating a new worker and a thread for it:
Workers[IndFreePlace] = new Worker(this->nextPendingConnection()->socketDescriptor(), IndFreePlace);
Threads[IndFreePlace] = new QThread(this);
Workers[IndFreePlace]->moveToThread(Threads[IndFreePlace]);
connect(Threads[IndFreePlace], &QThread::started, Workers[IndFreePlace], &Worker::StartWork);
connect(Workers[IndFreePlace], &Worker::StopThread, this, &Server::DisconnectClient);
Threads[IndFreePlace]->start();
delete Workers[ThreadID];
Workers[ThreadID] = nullptr;
Threads[ThreadID]->quit();
Threads[ThreadID]->wait();
delete Threads[ThreadID];
Threads[ThreadID] = nullptr;
void Worker::StartWork()
{
mUserSocket = new QTcpSocket;
mUserSocket->setSocketDescriptor(mSockD);
connect(mUserSocket, &QTcpSocket::disconnected, this, &Worker::StopWork);
qDebug() << "Check\n";
}
void Worker::StopWork()
{
qDebug() << "Client #" << mID << " disconnected\n";
mUserSocket->close();
delete mUserSocket;
mUserSocket = nullptr;
emit StopThread(mID);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question