What are use cases of separate arrive and wait of C++20 barrier?

|▌冷眼眸甩不掉的悲伤 提交于 2020-08-07 03:34:45

问题


C++20 std::barrier has arrive_and_wait method, which is what pretty much every synchronization barrier implementation has.

But it also has separate arrive and wait. I'm wondering what are the use cases.


回答1:


OK, so you've got a bunch of threads that have to do some kind of synchronized tasks. These tasks are grouped into phases: the tasks from one phase will use data produced by tasks from a previous phase, and all previous phase work must be done before any next-phase work can start. Any work that requires data from a previous phase shall be called "in-phase" work.

However, let's say that not everything you need to do actually requires data from a previous phase. There could be some individual work items that a thread could perform that doesn't read data from a previous phase. Let's call this "out-of-phase" work.

If you try to do this out-of-phase work before calling arrive_and_wait, then you could be blocking all of the other threads from doing something even through you are done with the actual work they're waiting on. Depending on the balance between in-phase and out-of-phase work, that could be a lot of wasted performance.

So if a thread has finished its in-phase work and has some out-of-phase work to do, it can arrive. This potentially frees up all of the other threads if they too are finished with their in-phase work. The thread can then go process some out-of-phase work potentially asynchronously with work being done from the next phase. Once the out-of-phase work is done, the thread can wait on the token generated by its call to arrive, which if the next phase has started, will return without blocking.

Indeed, if the amount of in-phase work is much less than the amount of out-of-phase work, then this pattern means that threads almost never block. The barrier just acts as a multi-thread atomic ordering operation, never a blocking one.



来源:https://stackoverflow.com/questions/63119302/what-are-use-cases-of-separate-arrive-and-wait-of-c20-barrier

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!