C++ fixed-size queue full/empty signaling
Use a semaphore to cause the producers to sleep when the queue is full, and another semaphore to cause the consumers to sleep when the queue is empty.
when the queue is neither full nor empty, the sem_post and sem_wait operations are nonblocking (in newer kernels)

#include &lt;semaphore.h&gt;

template&lt;typename lock_free_container&gt;
class blocking_lock_free
{
public:
    lock_free_queue_semaphore(size_t n) : container(n)
    {
        sem_init(&amp;pop_semaphore, 0, 0);
        sem_init(&amp;push_semaphore, 0, n);
    }

    ~lock_free_queue_semaphore()
    {
        sem_destroy(&amp;pop_semaphore);
        sem_destroy(&amp;push_semaphore);
    }

    bool push(const lock_free_container::value_type&amp; v)
    {
        sem_wait(&amp;push_semaphore);
        bool ret = container::bounded_push(v);
        ASSERT(ret);
        if (ret)
            sem_post(&amp;pop_semaphore);
        else
            sem_post(&amp;push_semaphore); // shouldn't happen
        return ret;
    }

    bool pop(lock_free_container::value_type&amp; v)
    {
        sem_wait(&amp;pop_semaphore);
        bool ret = container::pop(v);
        ASSERT(ret);
        if (ret)
            sem_post(&amp;push_semaphore);
        else
            sem_post(&amp;pop_semaphore); // shouldn't happen
        return ret;
    }
private:
    lock_free_container container;
    sem_t pop_semaphore;
    sem_t push_semaphore;
};