Is there anybody to help me bounded queue problem. Add a new member function to
ID: 3672286 • Letter: I
Question
Is there anybody to help me bounded queue problem.
Add a new member function to BoundedQueue called splitQueue.
template void BoundedQueue: :splitQueue (Integer pos, BoundedQueue& receivingQ) // alters self //1 preserves pos //! produces receivingQ //1 requires: pos selfl //! ensures : self=#selfle,pos) and receivingQ=#selflpos, l#self!) Here are some sample calls: // incoming : qx3 = qy4 = "red","or","ye"> qx3.splitQueue(1,qy4) // out going : qx3 qy4- // incoming : qx4 = qy5 qx4.splitQueue(e, ay5) // outgoing : qx4 = qy5 = qy6 "red","bk"> // incoming : qx5 qx5.splitQueue (4,gy6); // outgoing : qx5 = qy6 = > BoundedQueue is layered on StaticArray with internal data members contents and currentLength, as shown below private: I/ internal representation enum {lowerB = 0, upperB = (maxLength - 1)); typedef StaticArray1 ArrayOfT ArrayOfT contents; Integer currentLength;Explanation / Answer
//
// File: BoundedQueue.h
// Author: HARE KRISHNA
//
// Created on 27 February, 2016, 6:49 PM
//
#ifndef _BOUNDEDQUEUE_H
#define _BOUNDEDQUEUE_H
template<typename T>
class BoundedBlockingQueue
{
private:
std::queue<T> q;
boost::mutex mtx;
boost::mutex::condition_variable_any cond1; // q.empty() condition
boost::mutex::condition_variable_any cond2; // q.size() == size condition
int nblocked1;
int nblocked2;
bool stopped;
int size;
public:
BoundedBlockingQueue(int size)
: size(size), nblocked1(0), nblocked2(0), stopped(false)
{
if (size < 1)
{
// throws an Exception statement here
}
}
~BoundedBlockingQueue()
{
Stop(true);
}
bool Empty()
{
boost::mutex::scoped_lock lock(mtx);
return q.empty();
}
std::size_t Size()
{
boost::mutex::scoped_lock lock(mtx);
return q.size();
}
bool TryPush(const T& item)
{
boost::mutex::scoped_lock lock(mtx);
if (q.size() == size)
return false;
q.push(item);
lock.unlock();
cond1.notify_one();
return true;
}
void WaitPush(const T& item)
{
boost::mutex::scoped_lock lock(mtx);
++nblocked2;
while (!stopped && q.size() == size)
cond2.wait(lock);
--nblocked2;
if (stopped)
{
cond2.notify_all();
BOOST_THROW_EXCEPTION(BoundedBlockingQueueTerminateException());
}
q.push(item);
lock.unlock(mtx);
cond1.notify_one();
}
bool TryPop(T& popped)
{
boost::mutex::scoped_lock lock(mtx);
if (q.empty())
return false;
popped = q.front();
q.pop();
lock.unlock(mtx);
cond2.notify_one();
return true;
}
void WaitPop(T& popped)
{
boost::mutex::scoped_lock lock(mtx);
++nblocked1;
while (!stopped && q.empty())
cond1.wait(lock);
--nblocked1;
if (stopped)
{
cond1.notify_all();
BOOST_THROW_EXCEPTION(BoundedBlockingQueueTerminateException());
}
popped = q.front();
q.pop();
cond2.notify_one();
}
void Stop(bool wait)
{
boost::mutex::scoped_lock lock(mtx);
stopped = true;
cond1.notify_all();
cond2.notify_all();
if (wait)
{
while (nblocked1)
cond1.wait(lock);
while (nblcoked2)
cond2.wait(lock);
}
}
};
#endif /* _BOUNDEDQUEUE_H */
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.