简体   繁体   中英

Using std::thread in Node.js Addon

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.

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_function s 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_function s are short-lived.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM