C++ cppreference.com 谈论的是哪个线程池?

C++ Which thread pool is cppreference.com talking about?

我正在 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 试图告诉您标准允许在线程池中执行任务(而不是启动一个新线程来执行它)。尽管标准可能没有明确允许,但也没有什么可以禁止的。

我不知道有任何实现会为 std::async 使用线程池。

cppreference 和 C++ 标准实际上在这一点上存在分歧。 cppreference 是这样说的(我的强调和删除线):

The template function async runs the function f asynchronously (potentially optionally in a separate thread which may be part of a thread pool).

C++ standard 是这样说的:

If launch::async is set in policy, [std::async] calls [the function f] as if in a new thread of execution ...

而这显然是两个不同的东西。

只有 Windows' 的 std::async 实现使用线程池 AFAIK,而 gcc 和 clang 为每次调用 std::async 启动一个新线程(当 launch::async 是设置在policy), 从而遵循标准

这里有更多分析: