2015-10-26 57 views
1

我正在爲Node創建一個C++插件。我想多次運行uv_queue_work而不必睡主線程。任何想法如何做到這一點?如何多次使用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,但有內存泄漏,如果我繼續這樣運行,內存使用量不斷增加。但我還沒有找到另一種方式來做到這一點。所以,如果任何人有想法,我將不勝感激。

+0

我已經嘗試過使用NAN(這是[code](https://gist.github.com/oferh/bdde17c52f5bc97fdd42)) @ antirreni91您測試了哪個版本的節點? –

+0

打開節點問題[#3560](https://github.com/nodejs/node/issues/3560) –

+0

[gist](https://gist.github.com/oferh/bdde17c52f5bc97fdd42)代碼現在已更新要運行多次,問題可能是您在代碼中分配內存而不釋放它。 –

回答

0

我找到了解決方案!顯然,工作完成後,工人自己釋放記憶,所以我可以繼續這樣做。內存泄漏是我的代碼的錯,因爲我正在分配內存,而不是在其他地方釋放它(完全隱藏,我沒看到它),但現在它解決了。

謝謝!