From b48969563989cbc96a2f8756e2cb28a1f04da2c0 Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Sat, 25 May 2024 10:19:44 +0800 Subject: [PATCH] synchornize Competitive Companion Thread stop. --- RedPandaIDE/problems/competitivecompenionhandler.cpp | 12 ++++++++++-- RedPandaIDE/problems/competitivecompenionhandler.h | 2 ++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/RedPandaIDE/problems/competitivecompenionhandler.cpp b/RedPandaIDE/problems/competitivecompenionhandler.cpp index c9bd655e..59d6613a 100644 --- a/RedPandaIDE/problems/competitivecompenionhandler.cpp +++ b/RedPandaIDE/problems/competitivecompenionhandler.cpp @@ -56,7 +56,7 @@ void CompetitiveCompanionHandler::stop() return; connect(mThread, &QThread::finished, mThread, &QObject::deleteLater); - mThread->stop(); + mThread->waitStop(); mThread=nullptr; } @@ -155,6 +155,7 @@ CompetitiveCompanionThread::CompetitiveCompanionThread(QObject *parent): mStop{false}, mBatchProblemsRecieved{0}, mStartSemaphore{0}, + mStopSemaphore{0}, mStartOk{false} { } @@ -170,6 +171,12 @@ bool CompetitiveCompanionThread::waitStart() return mStartOk; } +void CompetitiveCompanionThread::waitStop() +{ + stop(); + mStopSemaphore.acquire(1); +} + void CompetitiveCompanionThread::run() { QTcpServer tcpServer; @@ -179,7 +186,7 @@ void CompetitiveCompanionThread::run() } mStartSemaphore.release(1); while(!mStop) { - tcpServer.waitForNewConnection(100); + tcpServer.waitForNewConnection(1000); while (tcpServer.hasPendingConnections()) { QTcpSocket* clientConnection = tcpServer.nextPendingConnection(); onNewProblemConnection(clientConnection); @@ -187,4 +194,5 @@ void CompetitiveCompanionThread::run() } } tcpServer.close(); + mStopSemaphore.release(1); } diff --git a/RedPandaIDE/problems/competitivecompenionhandler.h b/RedPandaIDE/problems/competitivecompenionhandler.h index 3ba3502e..a12e6098 100644 --- a/RedPandaIDE/problems/competitivecompenionhandler.h +++ b/RedPandaIDE/problems/competitivecompenionhandler.h @@ -33,6 +33,7 @@ public: CompetitiveCompanionThread &operator=(const CompetitiveCompanionThread&) = delete; void stop(); bool waitStart(); + void waitStop(); signals: void newProblemReceived(int num, int total, POJProblem newProblem); // void newBatchReceived(int total); @@ -48,6 +49,7 @@ private: int mBatchCount; int mBatchProblemsRecieved; QSemaphore mStartSemaphore; + QSemaphore mStopSemaphore; bool mStartOk; };