简体   繁体   English

在 scope 之外的本地 object 之后,异步成员 function 会发生什么情况?

[英]What happen to async member function after local object out of scope?

For example, if I can call an async function like this:例如,如果我可以像这样调用异步 function:

{
    Example example;
    example.asyncFuncInside();
}

Since the object is freed after the scope, will it have any risks on the ongoing task running inside the async function?由于 object 在 scope 之后被释放,它会对异步 function 内部运行的正在进行的任务有任何风险吗?

This cannot be answered in the current form.目前的形式无法回答这个问题。 If the "async" is a detached std::thread , nothing bad happens.如果“异步”是分离的std::thread ,则不会发生任何不好的事情。 If the async function was implemented using std::async , on the other hand, then asyncFuncInside would return its std::future , and since that's ignored the dtor of std::future would block.另一方面,如果异步 function 是使用std::async实现的,则asyncFuncInside将返回其std::future ,并且由于忽略了std::future的 dtor 将阻塞。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果你“删除这个”会发生什么; 在成员 function 中? - what will happen if you do “delete this;” in a member function? 成员函数什么时候超出范围? - When does a member function go out of scope? 指向(全局/持久)对象的指针超出范围后,对象(而非指针)的成员向量将“未设置” - Member vector of objects (not pointers) gets 'unset' after the pointer to (global/persisting) object goes out of scope 为什么在 object `ins` 超出 scope 之后,`std::function` 对象(即 `fn`)仍然可以正常工作? - Why the `std::function` object(i.e `fn`) still works well after the object `ins` is out of scope? 将有效的 class object 指针绑定到 boost::function&lt;&gt;fn。 如果在 object 被破坏后调用 `fn` 会发生什么? - Bind a valid class object pointer to boost::function<>fn. What may happen if invoking the `fn` after the object has been destoryed? 函数超出范围后,C++ 对象数据被清除 - C++ object data being wiped clean after function goes out of scope 在C ++函数内部定义的对象在超出范围后是否会被销毁? - Will an object defined inside a C++ function be destroyed after it goes out of scope? 如果我通过引用捕获局部变量会发生什么,并且它超出范围? - What happens if I capture a local variable by reference, and it goes out of scope? 与成员函数异步 - async with member function 使用引用成员超出范围 - Using a reference member out of scope
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM