0

Refering to https://github.com/cameron314/readerwriterqueue , there is a sample code like following :

ReaderWriterQueue<int> q(100);       // Reserve space for at least 100 elements up front

q.enqueue(17);                       // Will allocate memory if the queue is full
bool succeeded = q.try_enqueue(18);  // Will only succeed if the queue has an empty slot (never allocates)
assert(succeeded);

int number;
succeeded = q.try_dequeue(number);  // Returns false if the queue was empty

assert(succeeded && number == 17);

// You can also peek at the front item of the queue (consumer only)
int* front = q.peek();
assert(*front == 18);
succeeded = q.try_dequeue(number);
assert(succeeded && number == 18);
front = q.peek();
assert(front == nullptr);           // Returns nullptr if the queue was empty

Suppose I like to have a array for ReaderWriterQueue constructed with parameter 100 , how can I define the var ?!

ReaderWriterQueue<int> qp[1024] ;  

that is ok , but I like all 1024 of them with parameter 100 ,

ReaderWriterQueue<int>(100) qp[1024] ;

won't compiled , I try to use pointer :

ReaderWriterQueue<int>* qptr;
qptr = new ReaderWriterQueue<int>(1024) ;

will work , but without parameter 100 ,

qptr = new ReaderWriterQueue<int>(100) (1024) ;

won't compile , so how can I make an array of 1024's ReaderWriterQueue , all of them constructed with parameter 100 ?!

4
  • Can you use boost? Commented May 6, 2016 at 1:07
  • I prefer not to use boost , thanks for information . Commented May 6, 2016 at 1:14
  • That's a shame boost::optional used with boost::in_place would do it. Commented May 6, 2016 at 1:26
  • You could declare a char array large enough to hold the queues and then inplace construct them into the array. Commented May 6, 2016 at 2:40

3 Answers 3

1

As ReaderWriterQueue is neither copy-able/movable, you may use static_vector from http://en.cppreference.com/w/cpp/types/aligned_storage

template<class T, std::size_t N>
class static_vector
{
    // properly aligned uninitialized storage for N T's
    typename std::aligned_storage<sizeof(T), alignof(T)>::type data[N];
    std::size_t m_size = 0;

public:
    // Create an object in aligned storage
    template<typename ...Args> void emplace_back(Args&&... args) 
    {
        if( m_size >= N ) // possible error handling
            throw std::bad_alloc{};
        new (data + m_size) T(std::forward<Args>(args)...);
        ++m_size;
    }

    // Access an object in aligned storage
    const T& operator[](std::size_t pos) const 
    {
        return *reinterpret_cast<const T*>(data + pos);
    }

    // Delete objects from aligned storage
    ~static_vector() 
    {
        for (std::size_t pos = 0; pos < m_size; ++pos) {
            reinterpret_cast<const T*>(data+pos)->~T();
        }
    }
};

And then use it

static_vector<ReaderWriterQueue<int>, 1024> queues;

for (int i = 0; i != 1024; ++i) {
    queues.emplace_back(100)
}
Sign up to request clarification or add additional context in comments.

Comments

0

Doing this with vector is trivial:

std::vector<ReaderWriterQueue<int>> qp(1024, ReaderWriterQueue<int>(100));

This of course assumes that ReaderWriterQueue has a (public) copy constructor which does the right thing. However, this is not the case in the current version of the ReaderWriterQueue, so what you're trying to do is not possible.

1 Comment

thanks , but copy constructor is in private area , so it won't help
0

If you really want to use arrays, you can use std::fill to fill its elements as follows:

ReaderWriterQueue<int> qp[1024];
std::fill(begin(qp), end(qp), ReaderWriterQueue<int>(100));

As it's pointed out in the comments, this would work if ReaderWriterQueue is copyable, which seems that it is not. Then, another solution would be to declare array or vector of (smart) pointers to ReaderWriterQueue as follows, and initialize it later:

std::vector<std::shared_ptr<ReaderWriterQueue<int>>> qp(1024);
for(auto& x : qp)
    x = make_shared<ReaderWriterQueue<int>>(100);

3 Comments

ReaderWriterQueue has no default or copy constructor so this doesn't work.
Thanks , compile error , copy and assignment constructor is in private area .
Your vector of shared_ptr's has every element in the vector pointing at the same Queue, which is not what's wanted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.