Modification to “Implementing an N process barrier using semaphores”

南笙酒味 提交于 2019-12-05 20:34:32

Your pseudocode correctly returns barrier back to initial state. Insignificant suggestion: replace

barrier.signal()
if(count==0){ barrier.wait()}

with IMHO more readable

if(count!=0){ barrier.signal()} //if anyone left pending barrier, release it

However, there may be pitfalls in the way, barrier is reused. Described barrier has two states:

  1. Stop each thread until all of them are pending.
  2. Resume each thread until all of them are running

There is no protection against mixing them: some threads are being resumed, while other have already hit the first stage again. For example, you have bunch of threads, which do some stuff and then sync up on barrier. Each thread body would be:

while (!exit_condition) {
  do_some_stuff();
  pend_barrier(); // implementation from current question
}

Programmer expects, that number of calls for do_some_stuff() will be the same for all threads. What may (or may not) happen depending on timing: the first thread released from barrier finishes do_some_stuff() calculations before all threads have left the barrier, so it reentered pending for the second time. As a result he will be released along with other threads in the current barrier release iteration and will have (at least) one more call to do_some_stuff().

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