Subject: Re: [boost] [lockfree::fifo] Review
From: Chris M. Thomasson (cristom_at_[hidden])
Date: 2009-12-20 08:17:10


> > FWIW Tim, here is a very simple pseudo-code implementation of a nice
> > single-producer/consumer bounded queue:

> interesting ... it passes objects via pointers, though, which limits its
> usefulness ...

I believe that can be solved by doing something like this:

<pseudo-code typed in news reader>
___________________________________________________________
template<typename T, size_t T_depth>
struct spsc_queue
{
    struct cell
    {
        std::atomic<bool> m_busy; // = false;
        T m_object;
    };

    cell m_buffer[T_depth];
    size_t m_head; // = 0
    size_t m_tail; // = 0

    bool push(T const& object)
    {
        cell& c = m_buffer[m_head];

        if (c.m_busy.load(memory_order_acquire)) return false;

        c.m_object = object;

        c.m_busy.store(true, memory_order_release);

        m_head = (m_head == T_depth - 1) ? 0 : (m_head + 1);

        return true;
    }

    bool pop(T& object)
    {
        cell& c = m_buffer[m_tail];

        if (! c.m_busy.load(memory_order_acquire)) return false;

        object = c.m_object;

        c.m_busy.store(false, memory_order_release);

        m_tail = (m_tail == T_depth - 1) ? 0 : (m_tail + 1);

        return true;
    }
};
___________________________________________________________

> still, if it performs better than my spsc ringbuffer, it
> would be interesting to use it as template specialization for pointer
> types ...

Well, IMO, it should perform better because the producer and consumer are
not thrashing each other wrt the head and tail indexes.