c语言调用js,c – 通过事件发出调用JS函数

您不需要使用uv_queue_work,但LibUV的线程库的一部分是uv_async_ *方法,这是您在这种情况下理想的方法.初始化辅助线程时,还要执行uv_async_init以创建共享异步数据结构.此函数也通过回调调用,该回调将在您的其他线程发送消息时运行.该回调是您调用JS代码来触发事件的地方.

这里有一些半假的代码作为例子:

在你的线程init函数中从JS调用,带有一个回调arg:

void ThreadInit(const v8::Arguments &args){

// This is just an example, this should be saved somewhere that

// some_callback will be able to access it.

v8::Persistent<:function> js_callback = args[0].As();

// And save this where you'll be able to call uv_close on it.

uv_async_t async_data;

uv_async_init(uv_default_loop(), &async_data, some_callback);

// initialize the thread and pass it async_data

}

在线程中:

async_data.data = (void*) // Some data structure...

uv_async_send(&async_data);

在线程回调中:

void some_callback(uv_async_t *async_data){

// Note that this depending on the data, you could easily get thread-safety issues

// here, so keep in mind that you should follow standard processes here.

void* data = async_data->data;

// Process that data however you need to in order to create a JS value, e.g.

// Using NanNew because it is more readable than standard V8.

v8::Local count = NanNew(data.count);

v8::Local<:value> argv[] = {

count

};

js_callback->Call(NanNull(), 1, argv);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值