nodejs / node-addon-examples

Node.js C++ addon examples from http://nodejs.org/docs/latest/api/addons.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

thread_safe_function_round_trip please help

Talbot3 opened this issue · comments

hi , there is code not understand

bindings.startThread(item => {
  const thePrime = item.prime;
  console.log('The prime: ' + thePrime);

  // Answer the call with a 90% probability of returning true somewhere between
  // 200 and 400 ms from now.
  setTimeout(() => {
    const theAnswer = (Math.random() > 0.1);
    console.log(thePrime + ': answering with ' + theAnswer);
    bindings.registerReturnValue(item, theAnswer);
    if (!theAnswer) {
      bindings = null;
    }
  }, Math.random() * 200 + 200);
});

could anyone explain that why call registerReturnValue after 200ms~400ms

@OrangeBook the reason for the delay is only for illustration. The idea is that the return values arrive in an unpredictable order.

ok ,but the example will be abort at CallJs function last statement when delete setTimeout fucntion.Here is Doubt .

try again , that fine.