problem of jsb callback in multi-thread

Hi,everyone

is there a problem when JS_CallFunctionXXX into different thread?

i have read this two articles
http://my.oschina.net/u/816723/blog/138768
http://my.oschina.net/u/816723/blog/138949

as the second link, the JS_CallFunctionValue in onRequestCallback will generate a thread error in my case.

here is my js code and cpp code is same as the above articles

@
function callback()
{
cc.log(“in callback, arguments(”arguments.length“)=[”arguments“]”);
}

function main()
{
cc.log(“in jsb”);

var ret = hbb.CCPomelo.getInstance().connect(“127.0.01”, 3010);
if (ret != 0){
cc.log(“connect fail”);
return;
}

ret = hbb.CCPomelo.getInstance().request(“connector.entryHandler.entry”, “{sgi}”, this.callback);
if (ret != 0){
cc.log(“request fail”);
return;
}
}

main();
@

  • project info
    cc2dx 2.1.4
    iphone simulator sdk 6.1

thx
hbb

You should invoke js callback in the cocos2d thread.
This is the most simple way to do things.

But actually you may let js support multi-thread by creating more than one JSContext.
If you really want to do this, better check SpiderMonkey Docs in MDN.

hammer hammer wrote:

You should invoke js callback in the cocos2d thread.
This is the most simple way to do things.
>
But actually you may let js support multi-thread by creating more than one JSContext.
If you really want to do this, better check SpiderMonkey Docs in MDN.

thanks for your reply.
it works when I tried to invoke it in the cocos2d thread.
and you said that create a new JSContext maybe a better solution, but it’s a little diffcult to me (´Д`)
i read the JS_SetContextThread/JS_ClearContextThread doc in MDN, but the both functions not included in jsapi.h
anyway i have a workaround so far, thank you

The Spidermonkey libraries built for cocos2d-x disabled thread-safe support. So probably, you should not invoke Spidermonkey API in another thread.