hi
Does using QSemaphore::acquire()inside QThread's timerEvent ( QTimerEvent * event ) will freeze the application.
hi
Does using QSemaphore::acquire()inside QThread's timerEvent ( QTimerEvent * event ) will freeze the application.
Not by itself. If you experience a freeze, it is possible you have a deadlock in your program.
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:
#include <QtGui> const int BufferSize = 10; char buffer[BufferSize]; QSemaphore usedB; { public: void run() { startTimer(100); } { for (int i=0; i<BufferSize; i++) { freeB.acquire(); buffer[i]=i; usedB.release(); } } }; { public: void run() { for (int i=0; i<BufferSize; i++) { usedB.acquire(); qDebug()<<buffer[i]; freeB.release(); } } }; int main(int argc, char *argv[]) { Thread_write tw; tw.start(); Thread_read tr; tr.start(); return a.exec(); }To copy to clipboard, switch view to plain text mode
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.
Even after calling exec() in run() function ,the result is the same.
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?
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.
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