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:

  1. 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.

  2. work: work in worker thread , store output somewhere.

  3. resolve: in main thread, import output v8 structures , call callback.

details:

  1. 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.

  2. start thread in prefered way. use thread pool or not. don't use std::async not allowed wait it. pass pointer data , let work.

  3. from worker thread need in main thread. have using uv_async_send uv.h (see uv_async_init , uv_close). in main thread, in v8::handlescope, create v8::local<v8::function> , call it.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -