我能否在不受未来限制约束的前提下,直接运用std::async进行异步操作?

2026-04-16 22:5410阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计585个文字,预计阅读时间需要3分钟。

我能否在不受未来限制约束的前提下,直接运用std::async进行异步操作?

在异步模式下,我希望调用一些没有返回值的函数,而不必等待它们完成。例如,使用 `std::async` 时,在任务结束前,未来的对象不会损坏,这使我能够在我的特定情况下进行不同步的操作。例如:`void sendMail`。

高水平
我想在异步模式下调用一些没有返回值的函数而不等待它们完成.如果我使用std :: async,则在任务结束之前,未来的对象不会被破坏,这使得调用在我的情况下不同步.

void sendMail(const std::string& address, const std::string& message) { //sending the e-mail which takes some time... } myResonseType processRequest(args...) { //Do some processing and valuate the address and the message... //Sending the e-mail async auto f = std::async(std::launch::async, sendMail, address, message); //returning the response ASAP to the client return myResponseType; } //<-- I'm stuck here until the async call finish to allow f to be destructed. // gaining no benefit from the async call.

我的问题是

>有没有办法克服这个限制?
>如果(1)不是,我应该执行一次将采取那些“僵尸”期货并等待它们的线程吗?
>是(1)和(2)是否,是否有任何其他选项,然后只是建立我自己的线程池?

注意:
我宁愿不使用线程分离选项(由@ galop1n建议),因为创建一个新线程有一个我希望避免的开销.使用std :: async(至少在MSVC上)使用内部线程池.

谢谢.

您可以将未来移动到全局对象中,因此当本地future的析构函数运行时,它不必等待异步线程完成.

std::vector<std::future<void>> pending_futures; myResonseType processRequest(args...) { //Do some processing and valuate the address and the message... //Sending the e-mail async auto f = std::async(std::launch::async, sendMail, address, message); // transfer the future's shared state to a longer-lived future pending_futures.push_back(std::move(f)); //returning the response ASAP to the client return myResponseType; }

注:如果异步线程引用processRequest函数中的任何局部变量,则这不安全.

While using std::async (at least on MSVC) is using an inner thread pool.

这实际上是不符合的,标准明确表示使用std :: launch :: async运行的任务必须像在新线程中一样运行,因此任何线程局部变量都不能从一个任务持久存在.但通常并不重要.

我能否在不受未来限制约束的前提下,直接运用std::async进行异步操作?

标签:情况下

本文共计585个文字,预计阅读时间需要3分钟。

我能否在不受未来限制约束的前提下,直接运用std::async进行异步操作?

在异步模式下,我希望调用一些没有返回值的函数,而不必等待它们完成。例如,使用 `std::async` 时,在任务结束前,未来的对象不会损坏,这使我能够在我的特定情况下进行不同步的操作。例如:`void sendMail`。

高水平
我想在异步模式下调用一些没有返回值的函数而不等待它们完成.如果我使用std :: async,则在任务结束之前,未来的对象不会被破坏,这使得调用在我的情况下不同步.

void sendMail(const std::string& address, const std::string& message) { //sending the e-mail which takes some time... } myResonseType processRequest(args...) { //Do some processing and valuate the address and the message... //Sending the e-mail async auto f = std::async(std::launch::async, sendMail, address, message); //returning the response ASAP to the client return myResponseType; } //<-- I'm stuck here until the async call finish to allow f to be destructed. // gaining no benefit from the async call.

我的问题是

>有没有办法克服这个限制?
>如果(1)不是,我应该执行一次将采取那些“僵尸”期货并等待它们的线程吗?
>是(1)和(2)是否,是否有任何其他选项,然后只是建立我自己的线程池?

注意:
我宁愿不使用线程分离选项(由@ galop1n建议),因为创建一个新线程有一个我希望避免的开销.使用std :: async(至少在MSVC上)使用内部线程池.

谢谢.

您可以将未来移动到全局对象中,因此当本地future的析构函数运行时,它不必等待异步线程完成.

std::vector<std::future<void>> pending_futures; myResonseType processRequest(args...) { //Do some processing and valuate the address and the message... //Sending the e-mail async auto f = std::async(std::launch::async, sendMail, address, message); // transfer the future's shared state to a longer-lived future pending_futures.push_back(std::move(f)); //returning the response ASAP to the client return myResponseType; }

注:如果异步线程引用processRequest函数中的任何局部变量,则这不安全.

While using std::async (at least on MSVC) is using an inner thread pool.

这实际上是不符合的,标准明确表示使用std :: launch :: async运行的任务必须像在新线程中一样运行,因此任何线程局部变量都不能从一个任务持久存在.但通常并不重要.

我能否在不受未来限制约束的前提下,直接运用std::async进行异步操作?

标签:情况下