<small id='b8MQS'></small><noframes id='b8MQS'>

      <tfoot id='b8MQS'></tfoot>
      • <bdo id='b8MQS'></bdo><ul id='b8MQS'></ul>

      <legend id='b8MQS'><style id='b8MQS'><dir id='b8MQS'><q id='b8MQS'></q></dir></style></legend>

        <i id='b8MQS'><tr id='b8MQS'><dt id='b8MQS'><q id='b8MQS'><span id='b8MQS'><b id='b8MQS'><form id='b8MQS'><ins id='b8MQS'></ins><ul id='b8MQS'></ul><sub id='b8MQS'></sub></form><legend id='b8MQS'></legend><bdo id='b8MQS'><pre id='b8MQS'><center id='b8MQS'></center></pre></bdo></b><th id='b8MQS'></th></span></q></dt></tr></i><div id='b8MQS'><tfoot id='b8MQS'></tfoot><dl id='b8MQS'><fieldset id='b8MQS'></fieldset></dl></div>

      1. 从 node.js 本机代码调用回调

        时间:2023-10-17
            <i id='KQFI4'><tr id='KQFI4'><dt id='KQFI4'><q id='KQFI4'><span id='KQFI4'><b id='KQFI4'><form id='KQFI4'><ins id='KQFI4'></ins><ul id='KQFI4'></ul><sub id='KQFI4'></sub></form><legend id='KQFI4'></legend><bdo id='KQFI4'><pre id='KQFI4'><center id='KQFI4'></center></pre></bdo></b><th id='KQFI4'></th></span></q></dt></tr></i><div id='KQFI4'><tfoot id='KQFI4'></tfoot><dl id='KQFI4'><fieldset id='KQFI4'></fieldset></dl></div>
            <tfoot id='KQFI4'></tfoot>
            1. <small id='KQFI4'></small><noframes id='KQFI4'>

                • <bdo id='KQFI4'></bdo><ul id='KQFI4'></ul>

                  <legend id='KQFI4'><style id='KQFI4'><dir id='KQFI4'><q id='KQFI4'></q></dir></style></legend>
                    <tbody id='KQFI4'></tbody>
                  本文介绍了从 node.js 本机代码调用回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 C++ 为 node.js 编写附加组件.

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

                  这里有一些片段:

                  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));
                    }
                  

                  我需要将一个 javascript 函数保存为回调以供稍后调用.Client 类是另一个对象的观察者,应该从 onAsyncMethodEnds 调用 javascript 回调.不幸的是,当我调用函数BeInitiator"时,我在回调 Call() 之前收到Bus error: 10"错误

                  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()

                  感谢建议

                  推荐答案

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

                  You cannot ->Call from another thread. JavaScript and Node are single threaded and attempting to call a function from another thread amounts to trying to run two threads of JS at once.

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

                  You should either re-work your code to not do that, or you should read up on libuv's threading library. It provides uv_async_send which can be used to trigger callback in the main JS loop from a separate thread.

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

                  这篇关于从 node.js 本机代码调用回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:EnumDisplayMonitors 回调 下一篇:优点 &amp;回调(std::function/std::bind)与接口(抽象类)的缺点

                  相关文章

                • <small id='ujzig'></small><noframes id='ujzig'>

                    <bdo id='ujzig'></bdo><ul id='ujzig'></ul>
                  1. <i id='ujzig'><tr id='ujzig'><dt id='ujzig'><q id='ujzig'><span id='ujzig'><b id='ujzig'><form id='ujzig'><ins id='ujzig'></ins><ul id='ujzig'></ul><sub id='ujzig'></sub></form><legend id='ujzig'></legend><bdo id='ujzig'><pre id='ujzig'><center id='ujzig'></center></pre></bdo></b><th id='ujzig'></th></span></q></dt></tr></i><div id='ujzig'><tfoot id='ujzig'></tfoot><dl id='ujzig'><fieldset id='ujzig'></fieldset></dl></div>
                  2. <tfoot id='ujzig'></tfoot>
                    <legend id='ujzig'><style id='ujzig'><dir id='ujzig'><q id='ujzig'></q></dir></style></legend>