void WsThreadHelper::update(float dt) { WsMessage *msg = nullptr; // Returns quickly if no message _UIWsMessageQueueMutex.lock(); if (0 == _UIWsMessageQueue->size()) { _UIWsMessageQueueMutex.unlock(); return; } // Gets message msg = *(_UIWsMessageQueue->begin()); _UIWsMessageQueue->pop_front(); _UIWsMessageQueueMutex.unlock(); if (_ws) { _ws->onUIThreadReceiveMessage(msg); } CC_SAFE_DELETE(msg); }
void WsThreadHelper::update(float dt) { WsMessage *msg = nullptr; /* Avoid locking if, in most cases, the queue is empty. This could be a little faster. size() is not thread-safe, it might return a strange value, but it should be OK in our scenario. */ if (0 == _UIWsMessageQueue->size()) return; // Returns quickly if no message _UIWsMessageQueueMutex.lock(); if (0 == _UIWsMessageQueue->size()) { _UIWsMessageQueueMutex.unlock(); return; } // Gets message msg = *(_UIWsMessageQueue->begin()); _UIWsMessageQueue->pop_front(); _UIWsMessageQueueMutex.unlock(); if (_ws) { _ws->onUIThreadReceiveMessage(msg); } }
void WsThreadHelper::update(float dt) { WsMessage *msg = NULL; // Returns quickly if no message std::lock_guard<std::mutex> lk(_UIWsMessageQueueMutex); if (0 == _UIWsMessageQueue->size()) { return; } // Gets message msg = *(_UIWsMessageQueue->begin()); _UIWsMessageQueue->pop_front(); if (_ws) { _ws->onUIThreadReceiveMessage(msg); } CC_SAFE_DELETE(msg); }