node.js - Reuse Threads - C++ -
i making , addon (to nodejs). 1 of functions have responsible of doing fast algorithms audio arrives. objective algorithms in thread. resume of function:
void buffering(const functioncallbackinfo<v8::value>& args) { isolate* isolate = isolate::getcurrent(); handlescope scope(isolate); int size = args[1]->numbervalue(); int final_read = args[2]->numbervalue(); int inicio_read = args[3]->numbervalue(); int client_id = args[4]->numbervalue(); local<object> bufferobj = args[0]->toobject(); buf = node::buffer::data(bufferobj); char mini_buf[80000];//char mini_buf[4096]; memcpy(mini_buf, buf, size); //to implement thread int teste_buf = julius[client_id].audio_buffering(mini_buf, size, final_read, inicio_read, client_id); //(....returns nodejs...) }
if audio_buffering
executed 1 time, in way:
std::thread t[num__threads]; t[client_id] = std::thread(&srenginejulius::audio_buffering, &julius[client_id], mini_buf, size, final_read,inicio_read,client_id);
it happens that, function executed long audio coming (because of events in server). objective put executions in thread. same? if not how can make happen?
your call processed in main thread , therefore blocking js running. should doing work in thread, fundamentally change way function works right now.
essentially split function in 3 parts:
validation: copy input data (parameters, buffer data) out of v8 structures , off stack. can't call v8 functions in worker thread. function asynchronous need pass @ least 1 callback it.
work: work in worker thread , store output somewhere.
resolve: in main thread, import output v8 structures , call callback.
details:
i won't explain how data out, seems solved problem. have store somewhere out of v8 , current stack frame, suggest structure create
std::make_shared
, fill in while validate input. store callback(s)v8::persistent<v8::function>
; need store isolate.start thread in prefered way. use thread pool or not. don't use
std::async
not allowed wait it. pass pointer data , let work.from worker thread need in main thread. have using
uv_async_send
uv.h
(seeuv_async_init
,uv_close
). in main thread, inv8::handlescope
, createv8::local<v8::function>
, call it.
Comments
Post a Comment