Two parallel loops in cocos2d-x 3.17

I want to run two parallel loops in cocos2d-x 3.17 not waiting for each other to complete. One loop is the main scheduled loop updateScene() and other is pathFindingAlgo(). How can I run these two parallel loops continuously in two threads? The structure of code would be as follows. I want to acheive this because path finding algorithm takes about 1 to 2 sec to complete which is too expensive. It would be good if there is solution via openmp library. However, any library would be ok for solution.

bool Level::init()
{
this → schedule(schedule_selector(Level::updateScene));
}

void Level::updateScene(float delta)
{
// All game Objects and processes
}

void Level::PathFindingAlgo(Vec2 startPos, Vec2 endPos)
{
// path finding Algorithm
}

Have you tried using std::thread?

If you need to access the main thread from inside another thread, then you can use Director::getInstance()->getScheduler()->performFunctionInCocosThread().

Dear friend @R101 , I forgot to mention that:
void Level::PathFindingAlgo(Vec2 startPos, Vec2 endPos)
{
while(true)
{
// path finding Algorithm
}
}
path finding algorithm is inside a infinite loop to run continuously independent of updateScene(delta).
I tried - cocos2d::Director::getInstance()->getScheduler()->performFunctionInCocosThread(CC_CALLBACK_0(Level::PathFindingAlgo, this));
The game is stuck in the infinite loop which it should not. I tried with openmp but with no success. I have not tried std::thread.

You have misunderstood what I meant. You need to run your path finding algorithm inside a separate thread, which is the point of std::thread. performFunctionInCocosThread has nothing to do with that, and is used if you need to access the main cocos thread from the separate work thread that you have created.

If you’re going to do any multi-threading, then it’s best you brush up on the topic. There are plenty of examples of std::thread and performFunctionInCocosThread usage in both the cocos2d-x engine code and the cpp_tests project, along with a lot of information online on how to use std::thread.

1 Like

@R101 Thank You for your useful advice. I think I should learn more about std::thread and have look at cpp_tests. Hopefully, I will return back with solution.

Thanks to @R101… Now my game can run two parallel loops - one UpdateScene(delta) and other PathFindingAlgorithm() in two separate threads via std::thread. PathFindingAlgorithm() takes around 2 to 3 seconds to complete one cycle but my game runs with no lag. However, my device is heated too quickly. :sweat_smile: I want to post the solution as well which would be as follows:

bool Level::init()
{
this → schedule(schedule_selector(Level::updateScene));

std::thread path_find_thread = std::thread(&Level::PathFindingAlgo, this);
path_find_thread.detach();
}

// will continuously update in main thread
void Level::updateScene(float delta)
{
// All game Objects and processes
}

// will be in infinite while loop in another thread running path finding algorithm in every cycle of while loop
void Level::PathFindingAlgo()
{
while (true)
{
// path finding Algorithm
}
}