I do not know what the heck is wrong with my code that it never Dequeus the queue, instead isEmpty always meets true.
here's the queue class:
class ConcurrentQueue
{
private:
QQueue<QByteArray> dataStore;
public:
bool isEmpty();
private:
};
{
qDebug() << dataStore.length();
mutex.lock();
dataStore.enqueue(value);
mutex.unlock();
qDebug() << dataStore.length();
}
{
// mutex.lock();
// return dataStore.dequeue();
// mutex.unlock();
mutex.lock();
if(!dataStore.isEmpty())
{
tmp = dataStore.dequeue();
}
mutex.unlock();
return tmp;
}
bool ConcurrentQueue::isEmpty()
{
return dataStore.isEmpty();
}
class ConcurrentQueue
{
private:
QQueue<QByteArray> dataStore;
public:
void Enqueue(QByteArray value);
QByteArray Dequeue();
bool isEmpty();
private:
QMutex mutex;
};
void ConcurrentQueue::Enqueue(QByteArray value)
{
qDebug() << dataStore.length();
mutex.lock();
dataStore.enqueue(value);
mutex.unlock();
qDebug() << dataStore.length();
}
QByteArray ConcurrentQueue::Dequeue()
{
// mutex.lock();
// return dataStore.dequeue();
// mutex.unlock();
QByteArray tmp;
mutex.lock();
if(!dataStore.isEmpty())
{
tmp = dataStore.dequeue();
}
mutex.unlock();
return tmp;
}
bool ConcurrentQueue::isEmpty()
{
return dataStore.isEmpty();
}
To copy to clipboard, switch view to plain text mode
next a timer is connected to a slot to Enqueu a packet every lt's say 10 milliseconds:
//qDebug() << built.toHex();
qDebug() << "EnQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ";
queue.Enqueue(built);
QByteArray built((char*)data, len) ;
//qDebug() << built.toHex();
qDebug() << "EnQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ";
queue.Enqueue(built);
To copy to clipboard, switch view to plain text mode
A thread regularly checks if this queue is not empty it dequeues the queue and does something, but isEmplty always returns true!
if(queue.isEmpty())
{
qDebug() << "#####################################";
return;
}
//NEVER COMES HERE!
if(queue.isEmpty())
{
qDebug() << "#####################################";
return;
}
//NEVER COMES HERE!
QByteArray rd15Bytes = queue.Dequeue();
To copy to clipboard, switch view to plain text mode
here's the thread:
Write::Write()
{
}
void Write::run(){
while(1)
{
rs.writeToSerialPort(); // in this function above always isemplty is true
this->msleep(10);
}
}
Write::Write()
{
}
void Write::run(){
while(1)
{
rs.writeToSerialPort(); // in this function above always isemplty is true
this->msleep(10);
}
}
To copy to clipboard, switch view to plain text mode
Added after 31 minutes:
as I run the code, queue size increases and the contents are enQd inside of it. but I wonder why isEmpty always meets !!!!
Bookmarks