Results 1 to 9 of 9

Thread: Dequeue fails

  1. #1
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Dequeue fails

    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:
    Qt Code:
    1. class ConcurrentQueue
    2. {
    3. private:
    4. QQueue<QByteArray> dataStore;
    5.  
    6. public:
    7.  
    8. void Enqueue(QByteArray value);
    9. QByteArray Dequeue();
    10.  
    11. bool isEmpty();
    12. private:
    13. QMutex mutex;
    14. };
    15.  
    16. void ConcurrentQueue::Enqueue(QByteArray value)
    17. {
    18. qDebug() << dataStore.length();
    19. mutex.lock();
    20. dataStore.enqueue(value);
    21. mutex.unlock();
    22. qDebug() << dataStore.length();
    23. }
    24.  
    25. QByteArray ConcurrentQueue::Dequeue()
    26. {
    27.  
    28. // mutex.lock();
    29. // return dataStore.dequeue();
    30. // mutex.unlock();
    31.  
    32.  
    33. mutex.lock();
    34.  
    35. if(!dataStore.isEmpty())
    36. {
    37. tmp = dataStore.dequeue();
    38. }
    39.  
    40. mutex.unlock();
    41.  
    42. return tmp;
    43. }
    44.  
    45. bool ConcurrentQueue::isEmpty()
    46. {
    47. return dataStore.isEmpty();
    48. }
    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:
    Qt Code:
    1. QByteArray built((char*)data, len) ;
    2. //qDebug() << built.toHex();
    3. qDebug() << "EnQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ";
    4. 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!

    Qt Code:
    1. if(queue.isEmpty())
    2. {
    3. qDebug() << "#####################################";
    4. return;
    5. }
    6. //NEVER COMES HERE!
    7. QByteArray rd15Bytes = queue.Dequeue();
    To copy to clipboard, switch view to plain text mode 

    here's the thread:
    Qt Code:
    1. Write::Write()
    2. {
    3. }
    4.  
    5. void Write::run(){
    6. while(1)
    7. {
    8. rs.writeToSerialPort(); // in this function above always isemplty is true
    9.  
    10. this->msleep(10);
    11. }
    12. }
    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 !!!!
    Last edited by saman_artorious; 21st April 2013 at 09:16.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Dequeue fails

    Your queue is not thread-safe. Your isEmpty() implementation is incorrect.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Dequeue fails

    I dont understand! how can I resolve this?
    the isEmpty function works fine inside Queue Class, it even gives the correct number of elements already EnQd.
    But why does it fail when I call it from outside the class!
    Last edited by saman_artorious; 21st April 2013 at 10:26.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Dequeue fails

    Maybe because you're not protecting it from concurrent access.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Dequeue fails

    there is no need to protect it from concurrent access, it is a read-only function. On the other hand, EnQ and DeQ are
    well protected. I don't understand this at all, this must be working o'rite.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Dequeue fails

    Quote Originally Posted by saman_artorious View Post
    there is no need to protect it from concurrent access, it is a read-only function.
    No, that's wrong. You are supposed to protect data, not code. All access to your "datastore" member has to be protected by the same mutex. What if someone else is writing to the datastore while you're calling isEmpty()?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Dequeue fails

    no difference,
    Qt Code:
    1. bool ConcurrentQueue::is_Empty()
    2. {
    3.  
    4. mutex.lock();
    5. bool boo = dataStore->isEmpty();
    6. mutex.unlock();
    7. return boo;
    8. }
    To copy to clipboard, switch view to plain text mode 

    here is the output:

    "020503"
    EnQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ
    ##################################### true
    ##################################### true
    it enQs bytearray into queue whenever QTimer triggers, ok? next, another thread regularly calls a readfunction, at the beginning of this function I check if isEmpty is false, if false then I carry on with DeQing, ok? but the output shows it always returns true with ######


    Added after 9 minutes:


    if you need more info please let me know, I can't really find where I am doing wrong!


    Added after 46 minutes:


    I want answers, why is this doing this to me? I don't think there is a problem in my code.
    Last edited by saman_artorious; 21st April 2013 at 12:16.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Dequeue fails

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Dequeue fails

    probably op is doing something silly like making two instances of the queue without realising. Without FULL compilable example we are just guessing (see my sig...)
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. Replies: 2
    Last Post: 13th December 2011, 16:44
  2. Compiling of Qt4.6 fails
    By Wonko in forum Installation and Deployment
    Replies: 5
    Last Post: 18th February 2010, 16:18
  3. Qt 4.6.1 Compilation fails!
    By Diegol in forum Installation and Deployment
    Replies: 1
    Last Post: 10th February 2010, 16:36
  4. qt 4.5.2: make fails
    By unknown in forum Installation and Deployment
    Replies: 13
    Last Post: 3rd September 2009, 16:53
  5. Compiling QSA 1.2.0 fails
    By detheang in forum Installation and Deployment
    Replies: 11
    Last Post: 4th February 2006, 20:09

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.