如何多次使用 uv_queue_work?
How can I use uv_queue_work multiple times?
我正在为 Node.js 制作一个 C++ 插件。我想 运行 uv_queue_work 多次而不必休眠主线程。知道如何做到这一点吗?
到目前为止我已经这样做了:
void main(const FunctionCallbackInfo<Value>& args) {
//here goes my main code
//Here I schedule the worker, to run BEFOREmethod in a new thread, and AFTERmethod in the main thread
uv_queue_work(uv_default_loop(), req, BEFOREmethod,(uv_after_work_cb) AFTERmethod);
return callback;
}
void BEFOREmethod(uv_work_t* req){
//here goes the code that runs in new thread
usleep(200000);
}
void AFTERmethod(uv_work_t* req, int status){
//here goes the code that runs in main thread
//Then we schedule the uv_queue_work again
uv_queue_work(uv_default_loop(), req, BEFOREmethod,(uv_after_work_cb) AFTERmethod);
}
所以这行得通,我可以重新安排 uv_queue_work,但是存在内存泄漏,如果我保持这个 运行ning,内存使用量会不断增加。但我还没有找到另一种方法来做到这一点。所以如果有人有想法,我将不胜感激。
我找到了解决办法!显然工作完成后工作人员会自行释放内存,所以我可以继续这样做。内存泄漏是我的代码的错,因为我正在分配内存而不是在其他地方释放它(完全隐藏我没有看到它),但现在它已经解决了。
谢谢!
我正在为 Node.js 制作一个 C++ 插件。我想 运行 uv_queue_work 多次而不必休眠主线程。知道如何做到这一点吗?
到目前为止我已经这样做了:
void main(const FunctionCallbackInfo<Value>& args) {
//here goes my main code
//Here I schedule the worker, to run BEFOREmethod in a new thread, and AFTERmethod in the main thread
uv_queue_work(uv_default_loop(), req, BEFOREmethod,(uv_after_work_cb) AFTERmethod);
return callback;
}
void BEFOREmethod(uv_work_t* req){
//here goes the code that runs in new thread
usleep(200000);
}
void AFTERmethod(uv_work_t* req, int status){
//here goes the code that runs in main thread
//Then we schedule the uv_queue_work again
uv_queue_work(uv_default_loop(), req, BEFOREmethod,(uv_after_work_cb) AFTERmethod);
}
所以这行得通,我可以重新安排 uv_queue_work,但是存在内存泄漏,如果我保持这个 运行ning,内存使用量会不断增加。但我还没有找到另一种方法来做到这一点。所以如果有人有想法,我将不胜感激。
我找到了解决办法!显然工作完成后工作人员会自行释放内存,所以我可以继续这样做。内存泄漏是我的代码的错,因为我正在分配内存而不是在其他地方释放它(完全隐藏我没有看到它),但现在它已经解决了。
谢谢!