From 5ae5dbbd54baecff559cfc4d39a9aa0397bc840a Mon Sep 17 00:00:00 2001 From: James Chen Date: Mon, 6 Jun 2022 13:35:09 +0800 Subject: [PATCH] fixed #11339 : [v3.6] Crash after hot update by invoking cc.game.restart(). (#11348) * fixed #11339 : [v3.6] Crash after hot update. MessageQueue::terminateConsumerThread needs to wait consumer thead to exit. So don't detach consumer thead and we need to join the thread at the end of terminateConsumerThread. * Delete _consumerThead after join. --- native/cocos/base/threading/MessageQueue.cpp | 10 ++++++++-- native/cocos/base/threading/MessageQueue.h | 1 + 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/native/cocos/base/threading/MessageQueue.cpp b/native/cocos/base/threading/MessageQueue.cpp index 0363a97dc9..652aa639e9 100644 --- a/native/cocos/base/threading/MessageQueue.cpp +++ b/native/cocos/base/threading/MessageQueue.cpp @@ -124,8 +124,7 @@ void MessageQueue::runConsumerThread() noexcept { _reader.terminateConsumerThread = false; _reader.flushingFinished = false; - std::thread consumerThread(&MessageQueue::consumerThreadLoop, this); - consumerThread.detach(); + _consumerThread = ccnew std::thread(&MessageQueue::consumerThreadLoop, this); _workerAttached = true; } @@ -141,6 +140,13 @@ void MessageQueue::terminateConsumerThread() noexcept { kick(); event.wait(); + + if (_consumerThread != nullptr) { + if (_consumerThread->joinable()) { + _consumerThread->join(); + } + } + CC_SAFE_DELETE(_consumerThread); } void MessageQueue::finishWriting() noexcept { diff --git a/native/cocos/base/threading/MessageQueue.h b/native/cocos/base/threading/MessageQueue.h index 5606c5c45e..aba64308c2 100644 --- a/native/cocos/base/threading/MessageQueue.h +++ b/native/cocos/base/threading/MessageQueue.h @@ -179,6 +179,7 @@ private: bool _immediateMode{true}; bool _workerAttached{false}; bool _freeChunksByUser{true}; // recycled chunks will be stashed until explicit free instruction + std::thread *_consumerThread{nullptr}; friend class MemoryChunkSwitchMessage; };