kreuzerkrieg
kreuzerkrieg

Reputation: 3219

How to populate container with boost::lockfree::queue entities

Consider the following code

struct foo
{
    foo()
    {
        size_t someCalculatedValue = 2;
        bar.resize(someCalculatedValue*4);
        std::generate(bar.begin(), bar.end(), [&someCalculatedValue]() {return boost::lockfree::queue<int>(0xFFFF * someCalculatedValue); });
    }
    std::vector<boost::lockfree::queue<int>> bar;

};

which wouldnt compile, complaining about deleted copy constructor. The queue is non-copyable, which is ok, but looks like it also non movable? Am I missing something? Is there a way to fill stl container with these?
Of course, one can use something like below, if the capacity of 64k is enough.

struct boo
{
    using LocklessQueue = boost::lockfree::queue<int, boost::lockfree::capacity<0xFFFF-1>>;
    boo()
    {
        size_t someCalculatedValue = 2;
        bar = std::vector<LocklessQueue>(someCalculatedValue*4);
    }
    std::vector<LocklessQueue> bar;

};

Upvotes: 1

Views: 833

Answers (1)

sehe
sehe

Reputation: 393084

Locking primitives and lockfree objects are rarely movable.

This makes sense, because, by definition they're intended to be shared. When sharing, the object identity must stay the same, otherwise one party might move the object while the other is trying to still access it in the old location.

In the case of your code sample, I'm not convinced you have a need for lockless containers.

Upvotes: 2

Related Questions