std::async function running serially

后端 未结 3 1868
自闭症患者
自闭症患者 2021-01-24 04:22

When using std::async with launch::async in a for loop, my code runs serially in the same thread, as if each async call waits for the previous before launching. In the notes for

3条回答
  •  [愿得一人]
    2021-01-24 04:51

    As you noticed yourself, the future d-tor of future returned by std::async blocks and waits for the async operation to finish (for the future to become ready). In your case, cache object goes out of scope at each of the loop iterations and thus gets destructed, together with the future it holds, so you see the mentioned effect.

提交回复
热议问题