温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C++中怎么使用async启动并发任务

发布时间:2021-07-30 16:16:24 来源:亿速云 阅读:121 作者:Leah 栏目:大数据

这期内容当中小编将会给大家带来有关C++中怎么使用async启动并发任务,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

Reason(原因)

Similar to R.12, which tells you to avoid raw owning pointers, you should also avoid raw threads and raw promises where possible. Use a factory function such as std::async, which handles spawning or reusing a thread without exposing raw threads to your own code.

R.12告诉我们避免原始的所有权指针,本规则告诉我们:如果可能应该避免原始线程和promise。使用像std::async一样的工厂函数,它可以可以启动和重新使用线程而不必向外部代码保护原始线程。

Example(示例)

int read_value(const std::string& filename)
{
   std::ifstream in(filename);
   in.exceptions(std::ifstream::failbit);
   int value;
   in >> value;
   return value;
}

void async_example()
{
   try {
       std::future<int> f1 = std::async(read_value, "v1.txt");
       std::future<int> f2 = std::async(read_value, "v2.txt");
       std::cout << f1.get() + f2.get() << '\n';
   } catch (const std::ios_base::failure& fail) {
       // handle exception here
   }
}
Note(注意)

Unfortunately, std::async is not perfect. For example, it doesn't use a thread pool, which means that it may fail due to resource exhaustion, rather than queuing up your tasks to be executed later. However, even if you cannot use std::async, you should prefer to write your own future-returning factory function, rather than using raw promises.

不幸的是,std::async并不完美。例如,它没有使用线程池,这意味着它可能因为资源枯竭而失败,而不是将任务排队等候执行。然而,即使你不能使用std::async,你还是可以编写自己的返回future的工厂函数,而不是使用原始的promise。

Example (bad)(反面示例)

This example shows two different ways to succeed at using std::future, but to fail at avoiding raw std::thread management.

示例代码演示了两种不同的成功使用std::future的方式,但是没能避免使用原始的std::thread。

void async_example()
{
   std::promise<int> p1;
   std::future<int> f1 = p1.get_future();
   std::thread t1([p1 = std::move(p1)]() mutable {
       p1.set_value(read_value("v1.txt"));
   });
   t1.detach(); // evil

   std::packaged_task<int()> pt2(read_value, "v2.txt");
   std::future<int> f2 = pt2.get_future();
   std::thread(std::move(pt2)).detach();

   std::cout << f1.get() + f2.get() << '\n';
}
Example (good)(范例)

This example shows one way you could follow the general pattern set by std::async, in a context where std::async itself was unacceptable for use in production.

示例代码显示了你可以遵守的,由std::async设定的通常模式,这种模式可以用于开发过程中std::async无法直接使用的情况。

void async_example(WorkQueue& wq)
{
   std::future<int> f1 = wq.enqueue([]() {
       return read_value("v1.txt");
   });
   std::future<int> f2 = wq.enqueue([]() {
       return read_value("v2.txt");
   });
   std::cout << f1.get() + f2.get() << '\n';
}

Any threads spawned to execute the code of read_value are hidden behind the call to WorkQueue::enqueue. The user code deals only with future objects, never with raw thread, promise, or packaged_task objects.

任何启动并调用read_value的线程都被隐藏在WorkQueue::enqueue的调用后面。用户代码智能处理future对象,永远不会使用原始线程,promise或者打包的task对象。

上述就是小编为大家分享的C++中怎么使用async启动并发任务了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI