Results 1 to 8 of 8

Thread: QSemaphore::acquire() and QObject::timerEvent ( QTimerEvent * event )

  1. #1
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default QSemaphore::acquire() and QObject::timerEvent ( QTimerEvent * event )

    hi
    Does using QSemaphore::acquire()inside QThread's timerEvent ( QTimerEvent * event ) will freeze the application.

  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: QSemaphore::acquire() and QObject::timerEvent ( QTimerEvent * event )

    Not by itself. If you experience a freeze, it is possible you have a deadlock in your program.

  3. #3
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: QSemaphore::acquire() and QObject::timerEvent ( QTimerEvent * event )

    In the below program the Thread_write class writes data to a buffer every 100 milliseconds and the Thread_read reads the data from the buffer after the data has been written(this process will continue only 10 times).But even the timer is not started but the run function is executed ,where am i wrong.

    Qt Code:
    1. #include <QtGui>
    2.  
    3. const int BufferSize = 10;
    4. char buffer[BufferSize];
    5.  
    6. QSemaphore freeB(BufferSize);
    7. QSemaphore usedB;
    8.  
    9. class Thread_write : public QThread
    10. {
    11. public:
    12. void run()
    13. {
    14. startTimer(100);
    15. }
    16.  
    17. void timerEvent(QTimerEvent *)
    18. {
    19. for (int i=0; i<BufferSize; i++)
    20. {
    21. freeB.acquire();
    22. buffer[i]=i;
    23. usedB.release();
    24. }
    25. }
    26. };
    27.  
    28. class Thread_read : public QThread
    29. {
    30. public:
    31. void run()
    32. {
    33. for (int i=0; i<BufferSize; i++)
    34. {
    35. usedB.acquire();
    36. qDebug()<<buffer[i];
    37. freeB.release();
    38. }
    39. }
    40. };
    41.  
    42. int main(int argc, char *argv[])
    43. {
    44. QCoreApplication a(argc, argv);
    45. Thread_write tw;
    46. tw.start();
    47.  
    48. Thread_read tr;
    49. tr.start();
    50. return a.exec();
    51. }
    To copy to clipboard, switch view to plain text mode 

  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: QSemaphore::acquire() and QObject::timerEvent ( QTimerEvent * event )

    For timers to fire you need an event loop running in your thread. Your thread doesn't spin one - call QThread::exec(). Currently your writer thread does nothing and exits immediately.

  5. #5
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: QSemaphore::acquire() and QObject::timerEvent ( QTimerEvent * event )

    Even after calling exec() in run() function ,the result is the same.

  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: QSemaphore::acquire() and QObject::timerEvent ( QTimerEvent * event )

    Currently your design doesn't make much sense - the timerEvent will be fired only once and will then perform a loop preventing other events to be processed. You could as well move the loop to the run() method so both thread classes would be equivalent. What exactly are you trying to obtain?

  7. #7
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: QSemaphore::acquire() and QObject::timerEvent ( QTimerEvent * event )

    In the first thread ,i want to collect data from external source every 1000 milliseconds and store it in circular buffer.

    In the second thread, the data is fetched from the circular buffer and computation of the data is done which is time consuming.

  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: QSemaphore::acquire() and QObject::timerEvent ( QTimerEvent * event )

    In that case your producer thread is incorrect. You would be trying to write the data to the whole buffer every 100ms starting from the beginning each time. Store the current "first empty" and "first full" indexes to the buffer and write one item at a time. If you are using semaphores to synchronize threads, you don't need a timer, it won't work properly anyway. Simply run the producer function all the time you have new data to process. You might even use tryAcquire() so that you can return the flow elsewhere if the buffer is full.

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.