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