Previous | Next --- Slide 38 of 44
Back to Lecture Thumbnails
kayvonf

Question: Who can describe the problem with the code on this slide?

Abandon

The problem is that if there are consecutive barriers, the first thread entering the second barrier will firstly set b->flag to be 0 which may prevent other threads getting rid of the first barrier.

Master

The code may fail the case on the right-bottom part of the slide.

Suppose in the first barrier, the last arriver sets the flag, then threads will be able to proceed. If a thread meets the next barrier and set the flag to 0 while some other lagging threads are still in the while loop, then they actually didn't get through the first barrier.

MichaelJordan

If there is one barrier call after the other, the last arriver will set the flag to 1. This means other threads will be allowed to proceed into the next barrier call. Some threads may be slower than others or not scheduled to execute and still be sitting in the while loop of the first barrier call. However, since the flag has been set to 1, if any one of the threads leaves the barrier and goes into the second barrier call it may set the flag back to 0, keeping threads that did not leave the while loop forever stuck in the first barrier call. This can be fixed if threads can acknowledge when all other threads have entered/exited a barrier.