Is a threaded non-blocking circular buffer possible?
Posted: Thu Oct 02, 2014 2:13 am
When using a process with threads, is it possible to use a circular buffer that is not blocked by a mutex?
Even when using an atomic variable, I don't think its possible.
The problem as I see it is if one thread increments the atomic variable, and another thread also increments the variable, it's possible that both threads when interrogating the variable, could come up with the same value.
The best I could come up with was to also utilize a dimensioned array, dimensioned to the size of the circular buffer. Initially these would all be set to 0. Then dimArray(atomicVar) + 1 would be executed, and if and only if the result was 1, could the process then continue.
Thoughts?
Even when using an atomic variable, I don't think its possible.
The problem as I see it is if one thread increments the atomic variable, and another thread also increments the variable, it's possible that both threads when interrogating the variable, could come up with the same value.
The best I could come up with was to also utilize a dimensioned array, dimensioned to the size of the circular buffer. Initially these would all be set to 0. Then dimArray(atomicVar) + 1 would be executed, and if and only if the result was 1, could the process then continue.
Thoughts?