我正在cppreference.com上阅读std :: async的描述.
第一个描述说:
The template function async runs the function f asynchronously
(potentially in a separate thread which may be part of a thread
pool) and returns a std::future that will eventually hold the
result of that function call.
. [cppreference link]:std::async
cppreference.com正在谈论什么是线程池?
我阅读了标准草案N4713(C 17),并没有提到可能的线程池使用情况.
我也知道现在标准C中没有线程池.
解决方法:
事实上,cppreference和C标准是不一致的. cppreference说这个(强调和删除我的):
The template function
async
runs the functionf
asynchronously (potentiallyoptionally in a separate thread which may be part of a thread pool).
而C++ standard说:
If
launch::async
is set inpolicy
, [std::async
] calls [the function f] as if in a new thread of execution …
这些显然是两件不同的事情.
只有Windows的std :: async实现使用线程池AFAIK,而gcc和clang为每次调用std :: async启动一个新线程(当在策略中设置launch :: async时),因此遵循标准.
更多分析:https://*.com/a/50898570/5743288