【问题标题】:Wait for async calls to return before exiting program when using AWS-cpp-sdk使用 AWS-cpp-sdk 时,在退出程序之前等待异步调用返回
【发布时间】:2016-09-14 18:20:24
【问题描述】:

我正在做一个关于使用 AWS-cpp-sdk 的 POC 为此,我编写了一个简单的程序来将消息发送到 SQS 队列。

我正在使用 SendMessageAsync 方法发送如下消息。

sqsClient->SendMessageAsync(sendMessageRequest, &sendMessageCallBack);

我的程序崩溃了,因为我的程序在异步方法返回之前退出并且Aws::ShutdownAPI(options); 终止了由异步方法调用创建的线程。

我发现适用于 JAVA 的 AWS-sdk 正是针对这种情况提出了以下建议。 `

/**
 * Shuts down the client, releasing all managed resources. This includes
 * forcibly terminating all pending asynchronous service calls. Clients who
 * wish to give pending asynchronous service calls time to complete should
 * call getExecutorService().shutdown() prior to calling this method.
 */
@Override
public void shutdown() {
    super.shutdown();
    executorService.shutdownNow();
}`

我无法在 AWS cpp SDK 中找到等效的东西。 有人可以建议解决此问题的最佳方法吗?

【问题讨论】:

    标签: c++ multithreading amazon-web-services asynchronous


    【解决方案1】:

    您有责任确保在调用 ShutdownAPI() 之前完成您的请求。这通常只是在您直接在 main() 函数中执行操作的人为的“示例应用程序”类型的场景中的问题。您还需要确保在调用 ShutdownAPI 之前删除 SQS 客户端。

    On 选项是在退出前使用std::condition_variable(semaphore) 进行同步。您可以将信号量传递给您的回调,并在回调结束时传递notify_one()。然后,在关机之前,您可以在信号量上调用wait()

    【讨论】:

      猜你喜欢
      • 2014-11-26
      • 2019-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多