除同步请求外,阿里云C++ SDK还为您提供了异步处理接口。某个请求动作的异步处理接口通常为请求动作+Async,如describeInstancesAsync

以下代码展示了如何调用 DescribeInstances的异步请求接口获取指定地域所有ECS实例的详细信息。
#include <chrono>
#include <iostream>
#include <alibabacloud/core/AlibabaCloud.h>
#include <alibabacloud/ecs/EcsClient.h>

using namespace AlibabaCloud;
using namespace AlibabaCloud::Ecs;

int main(int argc, char** argv)
{
    // 初始化 SDK
    AlibabaCloud::InitializeSdk();
    // 配置 ecs 实例
    ClientConfiguration configuration("<your-region-id>");
    EcsClient client("<your-access-key-id>", "<your-access-key-secret>", configuration);
    // 创建API请求并设置参数
    Model::DescribeInstancesRequest request;
    request.setPageSize(10);
    client.describeInstancesAsync(request,
        [](const EcsClient*, const Model::DescribeInstancesRequest&,
          const EcsClient::DescribeInstancesOutcome&outcome,
          const std::shared_ptr<const AsyncCallerContext>&)
    {
        if (!outcome.isSuccess())
            // 异常处理
            std::cout << outcome.error().errorCode() << std::endl;
        else
            std::cout << "totalCount: " << outcome.result().getTotalCount() << std::endl;
    }, nullptr);
    // 执行其它操作
    using namespace std::chrono_literals;
    std::this_thread::sleep_for(5s);
    // 关闭 SDK
    AlibabaCloud::ShutdownSdk();
    return 0;
}