The following constructor can be used to
obtain a synchronization variable for a parallel
stride-1 loop with execution set
. In this constructor,
a new bit-array that
has one bit for each iteration
is obtained (viz. bit (i-low) belongs
to iteration i), and the lower bound l
is recorded:
Setting the bit of iteration i of a synchronization variable is done by calling the following synchronized instance method doPost() on this variable. Likewise, calling the synchronized instance method doWait() shown below on a synchronization variable blocks until the bit of iteration j of this variable becomes set. The test low <= j, however, makes this method non-blocking for out-of-bounds tests (which only may go backwards in the iteration space of the DOACROSS-loop):
The call to notifyAll() will eventually wake up all threads that are blocked on some synchronization variable (although, of course, they only re-acquire the lock of the monitor one at the time).
Because being notified does not necessarily mean that the appropriate bit actually has been set, however, the wait()-statement appears in a while-statement (rather than in an if-statement).
Although in the implementation shown above, waiting threads do not compete for processor time, the overhead of frequently accessing synchronized methods may be substantial. In this particular case, mutual exclusion while accessing the bit-array is not really required, and we can also implement the methods shown above using busy-waiting by eliminating the qualifiers synchronized and and replacing the wait()-statement by the call `Thread.currentThread().yield()'. In section 4.3 we will see that this approach can substantially improve the performance of a DOACROSS-loop. However, because the Java language specification does not guarantee fairness between runnable threads, and in Java there is no way to declare the elements of an array as volatile (and since the elements of bits are accessed asynchronously, other processors may fail to see changes [12, ch.17,]), this approach may be unsuited for particular implementations of the Java Virtual Machine.