Subj : Re: Lock-free buffer To : comp.programming.threads From : David Schwartz Date : Fri Mar 04 2005 12:09 pm "allan" wrote in message news:8f173f5e.0503041205.54114e7b@posting.google.com... >I am really curious if there exists the lock-free buffer algorithm. > Lock-free queue, lock-free list, etc. are there, but is there the > algorithm for the lock-free buffer? > > Lock-free queue is not sufficient for my case, because in the case > where three readers try to read a data and the lock-free queue has > only one data, only one of readers would get data with dequeue() > operation and the other two should wait until writers fill data into > the queue with enqueue() operation. In short, all readers should be > guaranteed to get the latest updated data all the time. > > If anybody advices me, it would be deeply appreciated. If you only have one writer and writes are infrequent, why not just do the following: 1) Allocate a new buffer. 2) Write the data into the buffer. 3) Save the pointer to the buffer that the readers use. 4) Atomically replace the pointer with the pointer to the new buffer. 5) Set a timer to free the old buffer later. If writes are frequent, the timers to free the old buffers might get ugly. In that case, you probably want an atomic reference count. DS .