使用async结束线程并超出其未来的范围是否安全?
Is it safe to shoot off a thread using async and go out the scope of its future?
我有一个函数,需要做一些即时的工作(重新分配),然后一些后台工作(移动旧数据),我可以这样做吗?
auto fof = std::async(std::launch::async, &cont2::move_data, this, old_data, old_size);
似乎工作得很好,但我很怀疑,因为它不会让我使用std::async通常没有东西来保持返回值,并尝试在同一线程上执行任务。
如果返回值std::future
超出作用域,则其析构函数将停止线程的执行,直到异步操作完成。
这意味着,是的,如果你不返回一个结果,它将同步;对std::async
的调用将返回一个临时对象,它将立即调用它的析构函数并阻塞,直到工作完成。类似地,如果将它绑定到一个值,它将继续工作,但一旦绑定值到达其作用域的末端,它将暂停。
{
// bind to a value
auto fof = std::async(std::launch_async,
&cont2::move_data, this, old_data, old_size);
// do work
} // at this point, execution will halt until cont2::move_data finishes
见http://en.cppreference.com/w/cpp/thread/async .
相关文章:
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- ";结果类型必须是可从输入范围的值类型""构造的;创建std::vector时
- 重载运算符的范围是什么?它是否会影响作为类成员的集合的插入函数?
- set_intersection范围范围
- 程序中的布尔函数返回输入的范围无论如何都是无效的
- 在C++中使用变量而不是"#define"来指定数组大小是不是一种糟糕的做法?(C错误:在文件范围内
- 在 c++ 中解决段树以外的范围查询的有效方法是什么?
- 这个变量在 C++ 中的范围是什么?
- 私有在函数定义/实现的返回值范围内是什么意思 (c++)?
- 满足标准::范围::范围概念
- 向量向量的范围范围
- 为什么这个循环变量在循环范围之外是可访问的
- 运算符重载中变量的数据类型和范围应该是什么
- 统一实际分布的范围限制是多少
- 用辅助方法范围范围的问题来操纵字符串
- 用于循环范围范围
- 在C++中实现随机预言机(输出在范围内是随机的,但对于相同的输入输出是相同的)
- 在C++11中,用什么方法对数组进行简单的范围管理是最干净的
- 矢量超出范围/范围检查
- 以一种漂亮的STL方式检查一个范围是否是另一个范围的子范围