Long Running AI Routine, Need help with multi-threading

Long Running AI Routine, Need help with multi-threading
0.0 0


I am making a game where the computer player runs a routine recursively to find the best move. As the moves get more complex this routine can take several seconds to complete. That’s fine except that it blocks the main thread and my animations stop until the routine is done.

I want to put that routine into a separate thread but can not find any useful information on how to do so in a cross-platform way.

Can someone who has successfully done multi-threading please provide advice on how to accomplish this. I need a method that will work across all cocos2d-x supported platforms.

It doesn’t have to be a separate thread, if there is a way to do this without it that would probably be even better.

I’m fairly new to this framework and so far I think it’s fantastic.



is the schedule method of no help?? you could try performing operations inside a custom method that you can schedule to be called at any given time interval and have your operations performed in that function.


Thanks for the suggestion, much appreciated. I had thought about using the scheduler but I was hoping there might be a better solution.

My main concern is that this routine calls itself over and over passing itself altered copies of the original arguments. This means that at each level of recursion there is a local variable with a unique value for that particular recursion. If I was to schedule the method to run at certain intervals, the method would have to leave prematurely, and once exited I would lose all of that information. Or I would have to store all of those values, creating a mess and wasting memory. At this point, I don’t see a way to have it work without the entire routine being allowed to complete in one shot. Does that make sense?

If I’m missing something and you have a way for schedules to work in this scenario please let me know. I am still curious to hear from anyone that has implemented process heavy recursive AI and how you accomplished that using cocos2d-x, or c++ in general if it is not platform specific.

Not that it matters I suppose, but just for the record I am using cocos2d-2.0-x-2.0.2



So I managed to figure out how to convert my recursive routine to a stack. I won’t bother posting all of the information again, the explanation as well as the source code is posted at http://gamedev.stackexchange.com/questions/37286/implement-negascout-algorithm-with-stack.

This is a great solution for anyone looking for a way to prevent a long running task in cocos2d-x from blocking without resorting to multi-threading. I did try to use pthread initially but couldn’t get it to work. I really wish someone would provide a detailed explanation on how to use pthread in cocos2d-x, with examples and indicate if it works on all supported platforms or not. In any case, this was my solution which was a LOT of work and I really hope that it helps someone else.



Wow! Great Stuff Thanks For Sharing


Just FYI cocos2d-x has pthread built-in. Do a search for pthread in the cocos2d-x folder and you should see libraries and includes.

//creating a thread
pthread_create(&networkThread, NULL, NetworkManager::networkThreadFunc, threaddata);

//defining the thread function
void* NetworkManager::networkThreadFunc(void* threaddata){…}

You’ll have to use a mutex while passing data. There is a lot of information on pthread of you search the interwebs.


I’m actually very happy with my solution, but of course it would be nice to have another option available.

Kevin, yes there is lots of info on pthread on the web, but an astonishing lack of information on how to use it with this framework, and absolutely nothing stating definitively that it will work cross-platform (beyond ios and android).

Since it is a part of the framework, a simple example in the test project would be much appreciated.

I can only see two files that use pthread in the entire cocos2d-x directory: CCTextureCache.cpp and HttpClient.cpp. I did not find them helpful in getting this to work.

Doing something as simple as including “pthread/pthread.h” in my header will fail to compile with the error:
“c:2d-2.0-x-2.0.2\cocos2dx\platform\third_party\win32\pthread\pthread.h(288): fatal error C1083: Cannot open include file: ‘sched.h’: No such file or directory”.

If you or anyone else has a simple working example of using pthreads in cocos2d-2.0-x-2.0.2 or later, can you please share your code and state what, if any extra steps are required to get it to work.



Here are my Visual Studio settings. The setup for Android/iOS is practically the same.

In the Project options under C/C++ > General > Additional Include Directories

Under Linker > Input > Additional Dependancies

Then in my program I include pthread with:
#include “pthread.h”

You should be able to use pthread afterwards.


Before I forget, I did manage to get pthread working and it was pretty straight forward once I knew how to properly set it up. Thanks Kevin, I never would have figured out the visual studio settings without your help.


My situation is the same and really need you guys to support.

I have a long running routine which only processes data and can take several seconds to complete but it doesn’t run recursively. I implement a countdown Timer in the scene, it counts normally until the above routine runs and blocks the timer to count down. The timer only comes back to work when the routine ends.

In my case, my routine is not the recursive one so i cannot convert it to a stack as notamonopoly did. I tried to put the routine to a std::thread but it doesn’t help and still blocks my timer.

Can you help me to find a way to run the routine and my countdownTimer simultaneously and the routine doesn’t block the timer?

Thank in advance


Hi all,

After using pthread instead of std::thread, my routine and timer can run simultaneously. I don’t know what differences between pthread and std::thread.

My problem now is that pthread uses a static function as its 3rd parameter while i need to use a non-static function for further development. I need to investigate more about it.


did you detach your std::thread?


Are you using latest cocos?
Then Did you know about AsyncTaskPool
Refer this thread it will help you,


Detaching std::thread solved my problem. Thanks Mr.slackmoehrle so much.