5

Imagine that I use a synchronous function, from my Node.js addon:

var check_ok = addon.my_function(parameters);
var final_results = addon.final_function(parameters);

But in the method code I have:

std::thread t[10]; //Global
//...
void my_function(const FunctionCallbackInfo<v8::Value>& args) {
//....
t[0] = thread(random_void_function, [parameters])
t[1] = thread(random_void_function_2, [parameters])
//...
}
//...
void final_results(const FunctionCallbackInfo<v8::Value>& args) {
//...
t[0].join();
t[1].join();
//...Give results.. etc
}

So I have 2 synchronous calls of addon, but in this addon two threads are used. One function will start the threads and the other will join them. The questions are: random_void_function and random_void_function_2 will run in parallel? Since my_function and final_function are synchronous, the random_void_function and random_void_function_2 will block the event loop? From what I see, they not block.

ZachB
  • 13,051
  • 4
  • 61
  • 89
zippo
  • 95
  • 1
  • 4
  • 1
    I know nothing of Node.JS, but from C++ point of view those two functions will run in parrallel and will not block anything (unless they call some blocking functions themselves). – SergeyA Jan 07 '16 at 19:58

1 Answers1

1

The questions are: random_void_function and random_void_function_2 will run in parallel?

Yes, provided they live long enough. If they are short-lived, an equally plausible outcome is that the first starts and exits prior to the second starting and exiting (or visa versa).

Since my_function and final_function are synchronous, the random_void_function and random_void_function_2 will block the event loop? From what I see, they not block.

No and maybe. addon.my_function will not block the event loop. addon.final_results (which I assume you meant to call above instead of addon.final_function) will only block if the random_void_functions are long- lived. If they are short lived and have already exited, addon.final_results will exit immediately.

Since you are not seeing any blocking, I suspect the random_void_functions are short-lived.

Andy
  • 1,663
  • 10
  • 17
  • My mistake.. `final_function` is an asynchronous function. That's why is not blocking. Considering the question as it is, I agree with you. – zippo Jan 07 '16 at 22:10