More on "Synchronization Barriers"


Suppose the Handy Board is executing the following program:

to hb-test
loop [
beep wait 10
//*
beep wait 30
//**
]
end
 

and the cricket is executing the following similar program:

to cr-test
loop [
beep wait 30
//*
beep wait 10
//**
]
end
 

How do we synchronize the two so that they beep at the same time? Assuming the two start the loop at the same time, after the first beep, the Handy Board will reach //* earlier than the Cricket and therefore make the second beep before the Cricket. Similarly, if they start the second beep at the same time, the Cricket will reach //** and make the first beep (through loop) earlier than the Handy Board. We need some kind of a barrier in those places to hold the flow of control such that the one who reaches the spot in the program first will wait until the other process has reached it, too. At this time, both processes proceed.

hb-synch and cr-synch defined below act as such barriers.

//sethere 50
//setgo 60
 
//load this into Handy Board
to hb-synch
loop [if (ir = here) [irsend go stop]]
end
 
//load this into the cricket
to cr-synch
send here
loop [ife (ir = go) [beep stop]]
end
 

The cricket sends out a here signal to the Handy Board once it enters the "critical zone" and won't proceed until it receives a go signal from the Handy Board. Similarly, the Handy Board will always check for here signal from the Cricket before it exits the "critical zone" and sends the go signal to the Cricket.

Using the above two procedures, true synchronization is achieved for the example we used at the beginning:

to hb-test
loop [
beep wait 10
hb-synch //in place of *
beep wait 30
hb-synch //in place of **
]
end
 
to cr-test
loop [
beep wait 30
cr-synch //in place of *
beep wait 10
cr-synch //in place of **
]
end
 

A subtle point that we learned later is that the "ir" command in Blue Dot Cricket Cricket Logo is the same as "next-ir" in Handy Logo, while in Red Dot Cricket Logo, "ir" is the same as "ir" in Handy Logo.

In our project, when programming the music on the Handy Board and the dance in Blue Dot Crickets, we could insert hb-synchs in the music and cr-synchs in the dance to achieve true synchronization.


back