September
2004, Issue 170
Test Your
EQ
|
Answer
6No
additional protections at all are required for a simple
queue such as this. Since the head and tail index updates
are implicitly atomic, the array of data items is already
sufficiently protected. Both processes examine both indices
to determine whether the queue contains data.
The
sending process checks to see whether the head index plus
one, modulo the length of the queue, equals the tail index.
If so, the queue is full, and it must wait to add more
data. Otherwise, it writes values to the data item indicated
by the head index and then increments it. The new data
item doesn’t become “visible” to the receiving process
until the increment occurs, so it doesn’t matter if the
data item is a complex structure that requires multiple
writes to update.
The
receiving process checks to see whether the two indices
are equal. If so, the queue is empty, and it must wait
before processing more data. Otherwise, it reads whatever
information it needs from the data item indicated by the
tail index and then increments it. The sending process
won’t reuse this slot in the array until the increment
occurs. So, once again, it doesn’t matter if the item
requires multiple accesses to extract the data.
Contributor:
David Tweed