cocos2d/cocos2d-x — pull request #9013 by RonTang (2014-11-05 03:25:14+00:00)
Title: CCScheduler should afford performTaskInCocosThread function.

//-------------------------------------------------------------------------
CCScheduler.h
Add code:
std::future performTaskInCocosThread(std::function &function);
std::vector> _tasksToPerform;
//-------------------------------------------------------------------------
CCSchedluer.cpp
modify CCSchedler::update()
if( !_tasksToPerform.empty()){
_performMutex.lock();
auto tempQueue = std::move(_tasksToPerform);//save queue,avoid deadlock
_tasksToPerform.clear();//maybe doesn't use it,because std::move() can do this
_performMutex.unlock();
for( auto &task : tempQueue ) {
auto tempTask=std::move(task);
tempTask();
}
}
/**
* a new method
***/
std::future Scheduler::performTaskInCocosThread(const std::function &function)
{
std::packaged_task task(function);
std::future res=task.get_future();

_performMutex.lock();

_tasksToPerform.push_back(std::move(task));

_performMutex.unlock();
return res;
}

//------------------------------------------------------------------------------
So we can use performTaskInCocosThread to do some function in cocosthread,that can tell us cocosthread has finished run the function that we pass.
The function return true mean normal exit,return false unnormal exit.
Sometimes we don't need return value,why not use void?
another reason return void is bug of Mircrosoft.
See that :
https://connect.microsoft.com/VisualStudio/feedback/details/791185/std-packaged-task-t-where-t-is-void-or-a-reference-class-are-not-movable
I intend to use git push this code,this is my first time to upload my code.
Prudence as well
Thanks a lot！！