How is an asynchronous callback performed?

How do all languages ​​implement asynchronous callbacks?

For example, in C ++, you need to start the "monitor stream" in order to start std::async. If it is running in the main thread, it must wait for the callback.

std::thread t{[]{std::async(callback_function).get();}}.detach();

vs

std::async(callback_function).get(); //Main thread will have to wait

What about asynchronous callbacks in JavaScript? In JS, callbacks are widely used ... How does V8 implement them? Does V8 create many threads to listen on them and call back when it receives a message? Or does he use one thread to listen to all callbacks and keep updating?

For instance,

setInterval(function(){},1000);
setInterval(function(){},2000);

Does V8 create 2 threads and monitor every callback state, or does it have a pool to track all callbacks?

+7
2

V8 ( setInterval). Engine JavaScript.

V8- setInterval JavaScript, ++-, , . , - . , . V8 engine ( V8 ) . , , . V8 , .

V8 . . , , .

/ Node.js/other embedder, .

+7

TL; DR: - , , . , , , , , , , .

, , , .

, . , . ( ) . 1:1. .

1.

, . , . , . ,

  1. ;

  2. , , , ;

  3. , . ;

  4. .

2.

, , , . , , . , , , , .

, , , , . , . .

3.

- . , . , , . , (, , ), , , .

(, ), , ( ) , . . .

4.

-, , ; - , . ( ). , ( ), , , . .

5.

, , , . .

, . , :

  1. : = + . , . , - , . , . . , .

  2. : , , . , - . "" , yield ( ). .

, , , . Golang yield/await, .

0

Source: https://habr.com/ru/post/1538843/


All Articles