节点.js C++插件:线程
Node.js C++ Addon: Threading
我正在为 Node.js (wxWidgets) 编写一个 GUI 插件,我想在自己的线程中运行 GUI 循环,因为我认为将其与 Node 的主线程和事件循环合并不是一个好主意。
但是我不确定如何创建新线程。我让它与uv_queue_work()
一起运行.但它不会为 GUI 创建独占线程,而是使用 Node 的线程池。这可能是一个坏主意,因为工作人员将在整个运行时留下来。(不确定这个)
我也可以使用wxWidgets的wxThread
,也可以工作。我在libuv git master中找到了一个新功能uv_thread_create
。不知道如何使用它,因为没有描述,此外它在 Node 稳定版本中尚不可用.js。
我的问题:创建多线程节点.js插件的"标准"方法是什么(如果有的话)?我查看了其他项目,但只能使用 libuv 找到运行时间较短的工作线程。
答案是,你通常希望通过将你的工作提交到 uv 事件队列来使用 Nodejs 管理的后台线程,然后让 nodejs 担心如何创建和管理线程。
下面是节点.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());
}
相关文章:
- 从不同线程使用int64的不同字节安全吗
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 在C++中使用cURL和多线程
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在cuda线程之间共享大量常量数据
- 如何将元素添加到数组的线程安全函数?
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 异常属于C++中的线程还是进程
- C++中的线程安全删除
- C++使用params创建线程函数会导致转换错误
- 类与私有变量的其他类之间的线程安全性
- 如何将带有缓冲区的对象从插件发送到节点线程安全
- 在另一个线程中调试插件
- 从多线程C++插件回调NodeJS Javascript函数
- 多线程插件体系结构中的共享、单侧可变状态
- 节点.js C++插件:线程
- 主线程和插件中的线程
- 多线程c++应用中的插件/模块通信方法