William Halchin wrote:
^^^ I am not happy at all with this legacy code. Very little
documentation and a lot of commented out experimental code. Anyway enough of
the kvetching. Adam, are you sure that the case of 1 producer and 1 consumer
is not a special degenerate case where synchronization is not necessary? Can
you show some counter-example to prove corruption? I have been trying to
convince myself that this degenerate case is not in some way special in not
needing synchronization. Question: would I put a mutex here? Answer: in a
An empty queue where the producer puts in the first element, signals the
no-empty transition, and then blocks waiting for the next element. The
consumer wakes, begins consuming the element (the only one in the list).
While this is happening, an event occurs, which wakes the producer
and since it’s higher priority, pre-empts the consumer mid-consumption.
Now the one and only element in the list is in a state of flux with
regards to the linked list (I’m assuming it’s a linked list of some
type) and the producer attempts to add an element to that list while
it’s in a state of flux - now you have undefined behaviour.
It’s hard to speak in concrete terms when I’m not sure what the data
structures look like and what the interface to them is. You could avoid
a lock using a read-copy-update paradigm. Or I guess you might be able
to work something such that the producer only works on one end of the
list and the consumer on the other and you somehow guarantee that the
queue never runs < 2 elements at any time when either the producer or
consumer needs to operate on the queue. This would be hard to do (and
more importantly hard to verify)
I know some people make assumptions that certain operations are atomic
(such as an assignment etc). This can be a very dangerous assumption
which falls under one or more of these criteria:
- a completely false assumption
- assumes the code will always be running on x86 (and banks on certain
- Works in uni-processor case, but not in an multi-processor one
FYI - There is an API for doing cross-platform atomic operations (check
out atomic_*() functions) safely.
With a PC, I always felt limited by the software available.
On Unix, I am limited only by my knowledge.
–Peter J. Schoenster <email@example.com>