Results 1 to 20 of 26

Thread: Thread Safe Queue container....

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #5
    Join Date
    Dec 2009
    Posts
    24
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Thread Safe Queue container....

    Quote Originally Posted by wysota View Post
    Producer/consumer problem is usually solved using semaphores or wait conditions, depending on the type of pattern you are dealing with (mainly if the number of producer "slots" is limited - i.e. when you produce data into a buffer of limited size).
    In this particular case, the container cannot be limited in size...even though it will likely have little "pressure" (1:1 - 3:1 consumers : producers, with each pop/push separated by seconds)... it is critical that the information produced by the producer is not lost....

    I ended up with:
    Qt Code:
    1. #ifndef ASYNC_queueH
    2. #define ASYNC_queueH
    3.  
    4. #include <QThread>
    5. #include <QQueue>
    6.  
    7. template<class T> class QAsyncQueue
    8. {
    9. public:
    10.  
    11. QAsyncQueue(uint _max = -1)
    12. : _max(max)
    13. {
    14. }
    15.  
    16. ~QAsyncQueue()
    17. {
    18. clean();
    19. }
    20.  
    21. uint count()
    22. {
    23. _mutex.lock();
    24. int count = _queue.count();
    25. _mutex.unlock();
    26. return count;
    27. }
    28.  
    29. bool isFull()
    30. {
    31. if (-1 == _max)
    32. return false;
    33.  
    34. _mutex.lock();
    35. int count = _queue.count();
    36. _mutex.unlock();
    37. return count >= max_;
    38. }
    39.  
    40. bool isEmpty()
    41. {
    42. _mutex.lock();
    43. bool empty = _queue.isEmpty();
    44. _mutex.unlock();
    45. return empty;
    46. }
    47.  
    48. void clean()
    49. {
    50. _mutex.lock();
    51. _queue.clear();
    52. _mutex.unlock();
    53. }
    54.  
    55. void push(const T& t)
    56. {
    57. _mutex.lock();
    58. _queue.enqueue(t);
    59. _mutex.unlock();
    60. }
    61.  
    62. T pull()
    63. {
    64. _mutex.lock();
    65. T i = _queue.dequeue();
    66. _mutex.unlock();
    67. return i;
    68. }
    69.  
    70. private:
    71.  
    72. QQueue<T> _queue;
    73. QMutex _mutex;
    74. int _max;
    75. };
    76. #endif
    To copy to clipboard, switch view to plain text mode 

    probably not state-of-the-art C++ code, but it seems to work... I declare one of these queue as a global or static and threads are sharing it....

    so from your answers so far, Qt is providing classic tools for threading...locking and such must be done by hand... there is no high level class hiding all this...

    thanks for the answers so far!
    Last edited by jcox23; 4th December 2009 at 09:32.

Similar Threads

  1. Is a QProcess thread safe in Qt4?
    By Jay_D in forum Qt Programming
    Replies: 4
    Last Post: 1st September 2009, 16:38
  2. What makes something not thread safe?
    By tgreaves in forum Newbie
    Replies: 9
    Last Post: 20th February 2009, 20:16
  3. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  4. Replies: 10
    Last Post: 20th March 2007, 22:19
  5. Are QHttp n QHttpRequestHeader thread safe?
    By Shambhavi in forum Qt Programming
    Replies: 4
    Last Post: 21st January 2006, 08:33

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
  •  
Qt is a trademark of The Qt Company.