Node.js C Addon:线程化
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Node.js C Addon:线程化,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3727字,纯文字阅读大概需要6分钟。
内容图文
![Node.js C Addon:线程化](/upload/InfoBanner/zyjiaocheng/708/fe05058499fd45f480e2880946426977.jpg)
我正在为Node.js(wxWidgets)编写一个GUI插件,我想在自己的线程中运行GUI循环,因为我不认为将它与Node的主线程和事件循环合并是个好主意.
但是我不确定如何创建新线程.我用uv_queue_work()运行了它.但它不会为GUI创建独占线程,而是使用Node的线程池.这可能是一个坏主意,因为工作人员将在整个运行时期间停留. (不确定这个)
我也可以使用wxWidgets的wxThread,也可以使用.我在libuv git master中找到了一个新函数uv_thread_create.不知道如何使用它,因为没有描述,除了它还没有在Node.js稳定版本中可用.
我的问题:如果有的话,创建多线程Node.js插件的“标准”方法是什么?我查看了其他项目,但只能使用libuv找到短时间运行的工作线程.
解决方法:
答案是您通常希望通过将您的工作提交到uv事件队列来使用Nodejs管理的后台线程,然后让nodejs担心如何创建和管理线程.
下面是node.js v0.10手册中缺少的样板示例.
struct Baton
{
// we need this structure to interact with the uv
// the uv_work_t must be the initial element and should store
// the callback function to be useful, but the rest
// is user defined depending on what is needed to actually do the work.
uv_work_t request;
v8::Persistent<v8::Function> callback;
// Add more elements to the structure as needed
int countdown;
};
static void AsyncTestWork (uv_work_t* req);
static void AsyncTestAfter(uv_work_t* req,int status);
static Handle<Value> AsyncTestPrep(const Arguments& args)
{
HandleScope scope;
if (args.Length() != 1) {
ThrowException(Exception::TypeError(String::New("Wrong number of arguments -- needs (callback)")));
return scope.Close(Undefined());
}
if (!args[0]->IsFunction()) {
ThrowException(Exception::TypeError(String::New("Wrong type of arguments -- needs (callback)")));
return scope.Close(Undefined());
}
v8::Local<v8::Function> callback = v8::Local<v8::Function>::Cast(args[0]);
Baton* baton = new Baton();
baton->request.data = baton;
baton->callback = v8::Persistent<v8::Function>::New(callback);
baton->countdown = 3;
uv_queue_work(uv_default_loop(), &baton->request, AsyncTestWork, AsyncTestAfter);
return scope.Close(v8::Undefined());
}
static void AsyncTestWork (uv_work_t* req)
{
// This method will run in a seperate thread where you can do
// your blocking background work.
// In this function, you cannot under any circumstances access any V8/node js
// valiables -- all data and memory needed, MUSt be in the Baton structure
Baton* baton = static_cast<Baton*>(req->data);
sleep(6); // some fictional work, delaying the return....
baton->countdown -= 1; // my actual work in this
}
static void AsyncTestAfter(uv_work_t* req,int status)
{
// This is what is called after the 'Work' is done, you can now move any data from
// Baton to the V8/Nodejs space and invoke call back functions
Baton* baton = static_cast<Baton*>(req->data);
v8::Handle<v8::Value> argv1[] = { v8::Null(), Number::New(baton->countdown) };
v8::Handle<v8::Value> argv2[] = { v8::Null(), Number::New(23) };
v8::TryCatch try_catch;
// Call back to the JS function, you can make as many/few callbacks
// as you need, they just go on the event loop queue for now.
// Note: that it is mostly customary to call the callback
// function just (exactly) which is probably what you want
// to do to avoid confusing your users if you make a public api
baton->callback->Call(v8::Context::GetCurrent()->Global(), 2, argv1);
baton->callback->Call(v8::Context::GetCurrent()->Global(), 2, argv2);
if (try_catch.HasCaught()) {
node::FatalException(try_catch);
}
if (baton->countdown > 0) {
// resubmit the worker for yet more work
uv_queue_work(uv_default_loop(), &baton->request, AsyncTestWork, AsyncTestAfter);
} else {
// we are finished, clean up and be done
baton->callback.Dispose();
delete baton;
}
}
void init(Handle<Object> exports)
{
exports->Set(String::NewSymbol("myAsyncTestFunction"),
FunctionTemplate::New(AsyncTestPrep)->GetFunction());
}
内容总结
以上是互联网集市为您收集整理的Node.js C Addon:线程化全部内容,希望文章能够帮你解决Node.js C Addon:线程化所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。