Solved

lockless Ring buffer in C

Posted on 2010-09-21
8
1,760 Views
Last Modified: 2012-05-10
Hi guys,

Another follow up from this post:

http://www.experts-exchange.com/Programming/Languages/C/Q_26488392.html

Basically, keeping the interface the same, can anyone provide a lockless implementation? i.e. one that does not need mutex or atomic counter updates.

I'm looking for a drop in replacement for the points :-)
0
Comment
Question by:PMembrey
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 32

Accepted Solution

by:
phoffric earned 500 total points
ID: 33731702
Polling with ms sleeps along with previous post in your question:

http:#33725666 indicates that atomicity is not required for ring/circular buffers.
You can get by with head/tail pointers.
   http://en.wikipedia.org/wiki/Circular_buffer#Circular_buffer_mechanics
   http://en.wikipedia.org/wiki/Circular_buffer#Full_.2F_Empty_Buffer_Distinction
The "Always Keep One Slot Open" is a simple solution that always keeps one slot unallocated. This includes an example implementation in C. (No thread issues here.)

Reading on, if you use an additional fill count (as your link suggests), then "This can require complex logic, especially if you are working with different threads."

But reading on, there are other solutions that do not have the thread issues.

There are other code solutions further down.
0
 

Author Comment

by:PMembrey
ID: 33731754
Yes, but I'm looking for a solution that I can drop-in in place of the current one - one that does not need a counter.
0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 500 total points
ID: 33732102
Ok. Note that the "Always Keep One Slot Open" solution does not use a counter. But, I'll see what others suggest. Always nice to see other solutions. If you remove the counter from your original link, then you have to make changes. In the end, you may end up with a solution similar to one of the ones suggested in the wiki link.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

Expert Comment

by:Infinity08
ID: 33752148
A completely lock-free circular buffer could be done if there is only one reader and one writer. If you have more than one reader and/or more than one writer, this becomes extremely hard, if not impossible.

I'm not sure what other mechanisms can be added than the ones that phoffric already pointed out. I personally like the solution that keeps one slot free, because it's simple and straightforward to implement.

Please don't award any points to this post, because I'm merely confirming what phoffric already said. I just posted, because you seemed to be waiting for further input before closing the question.
0
 

Author Closing Comment

by:PMembrey
ID: 33752699
Pity it can't be done but I couldn't see how it would work either. Thanks for confirmation.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 33752884
That's not what we said : it can be done in several ways. Refer to all the options presented by phoffric :)
0
 

Author Comment

by:PMembrey
ID: 33753709
A lockless ring buffer that is accessed by multiple reader and writer threads? :)
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 33753783
Ah, ok :)

I was basing my statement on what you said in the related question : "I have written a ring buffer and have one thread reading and one writing."
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

687 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question