std::async function running serially

后端 未结 3 1870
自闭症患者
自闭症患者 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

    That's a misfeature of std::async as defined by C++11. Its futures' destructors are special and wait for the operation to finish. More detailed info on Scott's Meyers blog.

    cache is being destroyed at the end of each loop iteration, thereby calling destructors of its subobjects.

    Use packaged_task or ensure you keep a container of copies of shared pointers to your cache to avoid waiting for the destructors. Personally, I'd go with packeged_task

提交回复
热议问题