
354 CHAPTER 5 Shared Memory Multiprocessors
on the flag associated with the barrier; if so, it writes the flag to release the p - 1
waiting processes. A simple attempt at a barrier algorithm may therefore look like
struct bar_type {
int counter;
struct lock_type lock;
int flag = 0;
} barename;
BARRIER (bar_name, p)
{
LOCK(bar^name·lock);
if (bar_name.counter == 0)
bar_name.flag =
0 ;
mycount = bar_name.counter++;
UNLOCK (barjame
. lock)
;
if (mycount == p) {
bar__name
.
counter = 0;
bar_name.flag =
1 ;
}
else
while (bar_name.flag ==0) {};
Preset flag if first to
reach*/
I* my count is a private variable*/
/Hast to arrive*/
l*reset counter for ...