What is async in thread | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

What is async in thread

Hello What should be criteria to opt for async over thread ? I assume when dependent shared data is there , we use thread and mutex. When it is not required to complete one task, like spelling suggestions in word file, we use thread and detach it... In which case async is preferred ?

23rd Feb 2021, 7:55 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
3 Answers
+ 1
Ketan Lalcheta std::async have 2 different launch policies launch::async and launch::deferred launch::async creates a new thread for different tasks, and you might be knowing that creating a joining threads frequently is not at all efficient (as thread creation is an expensive task especially on windows) launch::deferred on the other hand forces all tasks to be executed serially In either case you can see that problem will start occuring when working with tons of different tasks that are to be parallelized. The simple solution to this is using a threadpool and reusing the threads instead of creating new ones leading to improved performance and better resource management.
24th Feb 2021, 5:12 AM
Arsenic
Arsenic - avatar
+ 2
std::async is no where powerfull compared to thread. It is just (in words of Bjarne Stroustroup) " a simple way to handle simplest, rather common, cases " (similar case to that for range based for loops) I would use std::async only when I don't want to be bothered to do the heavy stuff manually in simpler programs. But when you want efficient parallelism, then going for a classical way of using threadpool is prefered.
23rd Feb 2021, 12:05 PM
Arsenic
Arsenic - avatar
+ 1
I am still sorry but could you please elaborate ? I got your point that thread is preferred over async for parellel processing ... Async does not giving simultaneously task performance ? If it gives , then why thread is so powerful ? I am sorry for this extended question
23rd Feb 2021, 6:05 PM
Ketan Lalcheta
Ketan Lalcheta - avatar