Proposal for v3.0: Thread helper utilities

cocos2d is not thread-safe, OpenGL operation is not thread-safe too. So you should run the codes that invoke cocos2d API or OpenGL API in the rendering thread which cocos2d runs in. It is very inconvenient to do it. So we want to provide a helper utilities to simplify it, such as:

// on rendering thread
createThread(threadFunc);

// a new thread
void threadFunc()
{
   // want to make a sprite run some action
   ThreadHelper::getInstance()->runOnGLThread(func);
}

// run on rendering thread
void func()
{
    sprite->runAction(xxx);
}

Because C++11 standard library supports thread and mutex well, so i don’t know what else is needed to be added into ThreadHelper class.
Any idea?

Well, In android project,I need to use JniHelper to call c*+ function in java class in order to refresh the game’s UI。 Well I can see the textures play wrong. It’s a mistake。 Reverse, I call Java function In c*+ code(I used alipay sdk to test this,click a ccmenu to show alipay’s UI), it’s also wrong. I think the games run in GLThread , but the Java Activity run in other thread. Can you give me some Solution ?

This thread is to discuss the requirement for ThreadHelper class.
Please create a new one to discuss your problem.
Thanks.

Any idea?

I don’t think we need a createThread. I am hoping that internally threads will be based on new C11 thread API. Cocos2dx users can directly create threads using C11 API (using lambdas etc), no need for a createThread wrapper that might be more restrictive (will need an explicit member function).

For those new to lambdas, creating a new thread using lambdas in C11 looks like this:
std::thread([](){ std::cout << "Hello from thread " << std::this_thread::get_id() << std::endl; }));
A runOnMainThread/runOnCocosThread/runOnGLThread function will be needed . But I would request a C-style function in cocos2d namespace. Internally it can use the threadHelper’s singleton instance but a direct C style function will keep code smaller.
Instead of:
cocos2d::ThreadHelper::getInstance->runOnGLThread.
Something like:
cocos2d::runOnCocosThread.
Also it will be great if this function can take lambdas as parameter just like C
11’s thread API.

@Sachin Garg
Yep, createThread is not needed, it is just in sample codes, not the need of ThreadHelper.
runOnGLThread will be static function, so the usage is

cocos2d::ThreadHelper::runOnGLThread(func);

And it supports c++11’s lambda.

Now the requirement is
* runOnGLThread.

Any other requirement?

Is this part of 3.0 alpha0 release? I can’t find this.

Is there a workaround for this that can work with 3.0 alpha0? Maybe something using event dispatcher?

Or even some android specific solution to run some code on cocos2dx nativeactivity/GL thread from Android’s main UI thread.

Sachin Garg: I’m not sure if ThreadHelper already exists or is only proposed, but this PR (already merged) might be your friend? :slight_smile:

Hello sir I was installing cocos2d and stuck at one place could you please help…

Actually I was following a cocos guide and at one place it was written that you hve to add NDK_ROOT I have entered but in next step it was written that you shold restart cygwin and type a command i did the same but haven’t got the same reult…

Could you please help

And also tell where i will find the name of packets of cygwin …
Must there some problem…:frowning: :frowning: :frowning:

In bin or somewhere else

Michael, what I can understand is that with your PR, now we can add Java Runnables to be executed on our nativeactivity’s main thread (which is also the GL thread). This, coupled with Android’s usual runOnUiThread gives full two way communication, we can now send runnables from both UI and GL thread to each other - But only in Java world.

What still seems missing is scheduling a C++ ‘runnable’ (or lambda function) to run on GL thread. This can be added easily following the same pattern that you used for Java runnables - we keep creating a thread safe list of lambda objects that are posted, and then run them 5 at a time in main loop.

Do I understand your patch correctly? or is my head going to explode soon trying to understand threads on Android? :stuck_out_tongue:

Nitesh, please create a new thread for your problem. This discussion is about multithreading support in cocos2dx. In your new post, please include the commands that you typed and the output that you see on screen, otherwise it will be difficult to tell what your problem might be. Also type ‘set’ command in cygwin and copy it’s output.

Again, do not reply to this message here, create a new thread about your problem.

Sorry about late reply. Because there are more high priority features to be implemented, so this issue has not been fixed. I will implement the feature in this version.

I sent a pull request for this issue https://github.com/cocos2d/cocos2d-x/pull/4401.
Any comment and suggestion is appreciated.

Thanks, looks good. I fixed my issue earlier using scheduler, will reimplement it using runOnGLThread now.

II suggest replace all the schedule_once stuff with a central dispatch, make this runOnUI/Main thread stuff a part of that. Generally the task dispatch should be able to run any task/lambda/func on any thread with repeated/once/timed requirements.

@Linus L
Sounds like a good idea.
Could you write some pseudo-code to describe it?

Thanks.

Not sure I’ll get the time to write it up since it’s gonna take some time to think it through. I’ll try though.

Thanks.