lock mutext to get element from g_queue

This commit is contained in:
minggo 2016-05-24 15:59:00 +08:00
parent ff01ba76dd
commit fff7a78efe
1 changed files with 34 additions and 22 deletions

View File

@ -1757,20 +1757,26 @@ void ScriptingCore::garbageCollect()
void SimpleRunLoop::update(float dt) void SimpleRunLoop::update(float dt)
{ {
g_qMutex.lock(); std::string message;
size_t size = g_queue.size(); size_t messageCount = 0;
g_qMutex.unlock(); while (true)
while (size > 0)
{ {
g_qMutex.lock(); g_qMutex.lock();
auto first = g_queue.begin(); messageCount = g_queue.size();
std::string str = *first; if (messageCount > 0)
g_queue.erase(first); {
size = g_queue.size(); auto first = g_queue.begin();
message = *first;
g_queue.erase(first);
--messageCount;
}
g_qMutex.unlock(); g_qMutex.unlock();
ScriptingCore::getInstance()->debugProcessInput(str); if (messageCount == 0)
break;
if (!message.empty())
ScriptingCore::getInstance()->debugProcessInput(message);
} }
} }
@ -1788,20 +1794,26 @@ void ScriptingCore::debugProcessInput(const std::string& str)
static bool NS_ProcessNextEvent() static bool NS_ProcessNextEvent()
{ {
g_qMutex.lock(); std::string message;
size_t size = g_queue.size(); size_t messageCount = 0;
g_qMutex.unlock(); while (true)
while (size > 0)
{ {
g_qMutex.lock(); g_qMutex.lock();
auto first = g_queue.begin(); messageCount = g_queue.size();
std::string str = *first; if (messageCount > 0)
g_queue.erase(first); {
size = g_queue.size(); auto first = g_queue.begin();
message = *first;
g_queue.erase(first);
--messageCount;
}
g_qMutex.unlock(); g_qMutex.unlock();
ScriptingCore::getInstance()->debugProcessInput(str); if (messageCount == 0)
break;
if (!message.empty())
ScriptingCore::getInstance()->debugProcessInput(message);
} }
// std::this_thread::yield(); // std::this_thread::yield();
std::this_thread::sleep_for(std::chrono::milliseconds(10)); std::this_thread::sleep_for(std::chrono::milliseconds(10));