C++中怎么使用async启动并发任务-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

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

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

创新互联建站主要为客户提供服务项目涵盖了网页视觉设计、VI标志设计、成都营销网站建设、网站程序开发、HTML5响应式网站建设公司移动网站建设、微商城、网站托管及网站维护、WEB系统开发、域名注册、国内外服务器租用、视频、平面设计、SEO优化排名。设计、前端、后端三个建站步骤的完善服务体系。一人跟踪测试的建站服务标准。已经为航空箱行业客户提供了网站设计服务。

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 f1 = std::async(read_value, "v1.txt");
       std::future 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 p1;
   std::future 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 pt2(read_value, "v2.txt");
   std::future 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 f1 = wq.enqueue([]() {
       return read_value("v1.txt");
   });
   std::future 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启动并发任务了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。


本文标题:C++中怎么使用async启动并发任务
网页URL:http://kswsj.cn/article/jdddhs.html

其他资讯