c++ 等待异步回调_回调vs承诺vs异步等待

c++ 等待异步回调

Why there is a multiple approach for handling asynchronous operations in Javascript? How do I choose the best fit for my use case? Lets deep dive to discuss on this topic. Even though myriads of post available on this topic. This story is intended to explain the concepts with simplified examples. If you are already aware of this concepts then its a refresher for you.

为什么在JavaScript中有多种方法来处理异步操作? 如何选择最适合自己的用例? 让我们深入讨论该主题。 即使有大量关于此主题的帖子。 这个故事旨在通过简化示例来解释这些概念。 如果您已经知道了这一概念,那么可以为您复习一下。

打回来 (Callback)

The callback is a function which is passed as a parameter to another function. The callback function is then executed perform some action. There are two types of callback function, they are:

回调是一个作为参数传递给另一个函数的函数。 然后执行回调函数,执行一些操作。 回调函数有两种类型,它们是:

  1. Synchronous Callback Function

    同步回叫功能

The synchronous callback functions are function which executes synchronously with out any blocking operation.

同步回调函数是在没有任何阻塞操作的情况下同步执行的函数。

In the following example, the function sayHello is invoked with text “Hello” and print function acts as a callback function. From the sayHello function, the text value is propagated to the callback function (print) and response is printed.

在以下示例中,函数sayHello与文本“ Hello”一起调用,而print函数充当回调函数。 从sayHello函数,文本值将传播到回调函数(打印)并打印响应。

function print(data) {
       
console.log(data)
}function sayHello(text, callback) {
callback(text)
}sayHello("Hello", print)

2. Asynchronous Callback

2.异步回调

The asynchronous callback functions are functions passed as an parameter for asynchronous operation and gets invoked once the operation completed.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ 中,多线程异步回调函数可以通过使用 std::thread 和 std::async 实现。 std::thread 是 C++11 中引入的一个线程库,它可以创建一个新的线程来执行函数。例如,以下代码创建了一个新线程并执行了一个函数: ``` void myFunction() { // 执行一些操作 } int main() { std::thread t(myFunction); t.join(); // 等待线程执行完毕 return 0; } ``` std::async 也是 C++11 中引入的另一个库,它可以异步地执行一个函数并返回一个 std::future 对象,该对象可以用于获取异步函数的返回值。例如,以下代码使用 std::async 异步地执行一个函数: ``` int myFunction() { // 执行一些操作 return 42; } int main() { std::future<int> result = std::async(std::launch::async, myFunction); // 执行一些其他操作 int value = result.get(); // 等待异步函数执行完毕并获取返回值 return 0; } ``` 在多线程中使用异步回调函数时,可以将回调函数作为参数传递给异步函数,在异步函数完成后调用回调函数。例如,以下代码异步执行一个函数并在完成后调用回调函数: ``` void myCallback(int result) { // 处理异步函数的结果 } void myFunction(std::function<void(int)> callback) { // 执行一些操作 int result = 42; callback(result); // 调用回调函数 } int main() { std::function<void(int)> callback = myCallback; std::async(std::launch::async, myFunction, callback); // 执行一些其他操作 return 0; } ``` 在上面的代码中,myFunction 接受一个 std::function 对象作为回调函数,并在异步执行完毕后调用该函数。在 main 函数中,我们创建了一个 std::function 对象并将其传递给异步函数。在异步函数执行完毕后,myCallback 函数将被调用,并传递异步函数的结果作为参数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值