[gameprogrammer] Re: Single producer consumer

>>I updated my single producer consumer to work even for non-atomic
reads and writes.

http://www.rakkar.org/sourcecode/SingleProducerConsumer.h.txt

[...]

There are quite a few operations in there... Have you considered every single combination of context switching patters? Keep in mind that in an SMP system, you could literally see the reader and writer executing their respective critical code sections at the very same time, resulting in various word-by-word alternating access patterns.

These are the only critical operations there, critical as in possibly concerning the other thread. Every other operation only happens in one thread ever.


Setting the read & write bools
Comparing the read & write bools
Setting the read & write pointers
Comparing the read & write pointers

Both cases are the same, setting can conflict with reading, resulting in reading the wrong value. Setting happens in the same thread for each respective variable, so will never be wrong.

Here are all the possible permutations for the (valid bool && valid pointer) comparison.

1. Correctly indicating you can't update the pointer
In this case, the read will return nothing and the write will do a new allocation.


2. Incorrectly indicating you can't update the pointer
In this case, the read will return when data is actually ready, resulting in a waste of one cycle. It will work the next cycle. The write will do a new allocation, resulting only in a larger pool than what is strictly necessary.


3. Incorrectly indicating you can update the pointer
This is impossible because the only way this can happen is if both the bool and the pointer were both wrong at the same time. Since they are volatile, and hence written immediately, it can't happen.


4. Correctly indicating you can update the pointer
Works as it should.


--------------------- To unsubscribe go to http://gameprogrammer.com/mailinglist.html


Other related posts: