Boost logo

Boost :

Subject: [boost] [lockfree] crash in queue
From: Christophe Henry (christophe.j.henry_at_[hidden])
Date: 2013-01-24 09:07:46


Hi Tim,

I tried using lockfree::queue but I keep getting a crash. My configuration:
boost 1.53 beta1, g++ 4.5 / clang (from trunk, a few weeks old), both
in c+11, i7 6 cores / 12 threads.

My test program is pretty much your example, except that my data is
not an int but a boost::function.
As a note, the crash seems to happen only when I have many consumers.
Many producers and one consumer seem to be ok.

Am I doing something wrong?

Thanks,
Christophe

#include <boost/thread/thread.hpp>
#include <boost/lockfree/queue.hpp>
#include <boost/function.hpp>

#include <boost/atomic.hpp>

boost::lockfree::queue<boost::function<void()> > lfqueue(10000);
boost::atomic<bool> done (false);

struct empty_task
{
    void operator()()const
    {
    }
};

void producer(void)
{
    for (int i = 0; i != 10000000; ++i)
    {
        boost::function<void()> task = empty_task();
        while (!lfqueue.push(task))
        {
            boost::this_thread::yield();
        }
    }
}

void consumer()
{
    while (!done)
    {
        boost::function<void()> res;
        if (!lfqueue.pop(res))
        {
            boost::this_thread::yield();
        }
        else
        {
            res();
        }
    }
}
void test_lockfree_queue()
{
    boost::thread_group producer_threads, consumer_threads;
    for (int i = 0; i != 1; ++i)
        producer_threads.create_thread(producer);

    for (int i = 0; i != 12; ++i)
        consumer_threads.create_thread(consumer);

    producer_threads.join_all();
    done = true;
    consumer_threads.join_all();
}

int main()
{
    test_lockfree_queue();
    return 0;
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk