I wanted to know if there are any fundamental differences / advantages of std :: async (with the launch option) and std :: thread? What is better to use and what, perhaps, has less overhead when creating?
1 answer
The std::thread used to run a function in a separate thread.
The std::async function also allows you to perform a function in another thread + returns the future result of std::future and allows you to more flexibly customize the process of multi-threaded execution (including through the use of the std::launch::async and std::launch::deferred flags std::launch::deferred ).
What is better to use?
Depends on the specific situation. In one case, it is sufficient to use std::thread , in others, to use the results of asynchronous computing.
What, perhaps, has a smaller overhead charge at creation?
I think it would be incorrect to compare std::async and std::thread in the performance plane. std::async is essentially a higher-level API that can be used by a thread pool in an implementation.
- Thanks for the answer, but I'm not interested in the flexible process. That is why I indicated both the title and the
launchissue. In this question, I am only interested in multithreading. At the same time, I did not specify a specific axis. But for that matter,linuxmore interested. I would like more specifics: for example, in this case we get such advantages / disadvantages, and in this - such. - Max ZS - Then I do not quite understand what you want.
std::asyncuses the samestd::threadinside (at least in gcc). - Pavel Parshin - I want an answer to the question. And if there is no difference, then the answer to the question will sound
"нет никакой разницы"."нет никакой разницы"I do not put the question"зачем и то и другое"."зачем и то и другое"Well, if there is a difference (you mentioned some kind of thread pool), then what is it and in whose, so to speak, benefit. I did not put into my question any hidden meaning and trick (no need to search for them and think out). As my question reads, this is how I understand it.PS: у меня нет в тегах gcc. А есть c++PS: у меня нет в тегах gcc. А есть c++- Max ZS