Merge pull request #15705 from minggo/jsb-debug-issue

lock mutext to get element from g_queue
This commit is contained in:
minggo 2016-05-25 13:55:34 +08:00
commit c8c57696be
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();
messageCount = g_queue.size();
if (messageCount > 0)
{
auto first = g_queue.begin(); auto first = g_queue.begin();
std::string str = *first; message = *first;
g_queue.erase(first); g_queue.erase(first);
size = g_queue.size(); --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();
messageCount = g_queue.size();
if (messageCount > 0)
{
auto first = g_queue.begin(); auto first = g_queue.begin();
std::string str = *first; message = *first;
g_queue.erase(first); g_queue.erase(first);
size = g_queue.size(); --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));