Is a threaded non-blocking circular buffer possible?

Everything else that doesn't fall into one of the other PB categories.
User avatar
RichAlgeni
Addict
Addict
Posts: 935
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Is a threaded non-blocking circular buffer possible?

Post by RichAlgeni »

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?
User avatar
idle
Always Here
Always Here
Posts: 6238
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Is a threaded non-blocking circular buffer possible?

Post by idle »

you should be able to do it with semaphores which only wait when they have to
Windows 11, Manjaro, Raspberry Pi OS
Image
Post Reply