1

I'm writing an add-on for node.js using c++.

here some snippets:

class Client : public node::ObjectWrap, public someObjectObserver {
public:
  void onAsyncMethodEnds() {
    Local<Value> argv[] = { Local<Value>::New(String::New("TheString")) };
    this->callback->Call(Context::GetCurrent()->Global(), 1, argv);
  }
....
private:
  static v8::Handle<v8::Value> BeInitiator(const v8::Arguments& args) {
    HandleScope scope;
    Client* client = ObjectWrap::Unwrap<Client>(args.This());

    client->someObject->asyncMethod(client, NULL);

    return scope.Close(Boolean::New(true));        
  }      

  static v8::Handle<v8::Value> SetCallback(const v8::Arguments& args) {
    HandleScope scope;
    Client* client = ObjectWrap::Unwrap<Client>(args.This());
    client->callback = Persistent<Function>::New(Handle<Function>::Cast(args[0]));

    return scope.Close(Boolean::New(true));
  }

I need to save a javascript function as callback to call it later. The Client class is an observer for another object and the javascript callback should be called from onAsyncMethodEnds. Unfortunately when I call the function "BeInitiator" I receive "Bus error: 10" error just before the callback Call()

thanks in advice

4

1 回答 1

3

你不能->Call从另一个线程。JavaScript 和 Node 是单线程的,试图从另一个线程调用一个函数相当于试图同时运行两个 JS 线程。

您应该重新编写代码以不这样做,或者您应该阅读libuv的线程库。它提供了uv_async_send可用于从单独的线程触发主 JS 循环中的回调。

这里有文档:http: //nikhilm.github.io/uvbook/threads.html

于 2013-06-13T15:40:47.657 回答