Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use async to start concurrent tasks in C++

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to use async to start concurrent tasks in C++. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

Reason (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 tells us to avoid raw ownership pointers, and this rule tells us that raw threads and promise should be avoided if possible. Using factory functions like std::async, it can start and reuse threads without protecting the original thread from external code.

Example (sample)

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report