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.

1
  • 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). Commented Jan 7, 2016 at 19:58

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

My mistake.. final_function is an asynchronous function. That's why is not blocking. Considering the question as it is, I agree with you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.