Results 1 to 6 of 6

Thread: Qthread n QTimer Problem

  1. #1
    Join Date
    Apr 2006
    Posts
    122
    Thanks
    18
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Qthread n QTimer Problem

    hi
    i am trying to print a no. o points on canvas. These points are read from a fifo through a clas MyThread.
    Qt Code:
    1. class MyThread: public QThread
    2. {
    3. Q_OBJECT
    4. public:
    5. MyThread(QObject *parent=0);
    6. ~MyThread();
    7. double x;
    8. double y;
    9.  
    10. protected:
    11. void run();
    12. signals:
    13. void dataAvailable();
    14. public slots:
    15. void startThread();
    16.  
    17. };
    18.  
    19. #endif
    To copy to clipboard, switch view to plain text mode 
    amd its implementation is as
    Qt Code:
    1. #ifndef MYTHREAD_cpp
    2. #define MYTHREAD_cpp
    3. #include <stdio.h>
    4. #include <sys/types.h>
    5. #include <sys/stat.h>
    6. #include <fcntl.h>
    7. #include <unistd.h>
    8. #include "MyThread.h"
    9. #include <iostream.h>
    10.  
    11.  
    12. MyThread::MyThread(QObject *parent) :QThread(parent)
    13. {
    14.  
    15. }
    16.  
    17. MyThread::~MyThread()
    18. {
    19. }
    20.  
    21. void MyThread::run()
    22. {
    23. int fd0;
    24.  
    25.  
    26. fd0 = open("/home/prince/mydev01",O_RDWR);
    27. if(fd0== -1)
    28. cout<<"\n------------------- No fifo found///////---------------------\n\n";
    29. else
    30. cout<<" Fifo founf \n";
    31.  
    32. read(fd0,&x,sizeof(int));
    33. read(fd0,&y,sizeof(int));
    34. printf("\nReceived x = %d & y = %d from RTCore\n",
    35. x,y);
    36. close(fd0);
    37. emit dataAvailable();
    38. }
    39. void MyThread::startThread()
    40. {
    41. start(LowPriority);
    42. }
    43.  
    44.  
    45. #endif
    To copy to clipboard, switch view to plain text mode 

    I need to refresh view every second. So a Timer generates a signal everysecond to run some code to claa slot MyThread::startThread(). Control reaches and it also goes to run(). But either it donot enters run() ( i checked it with gdb breakpoint method) or before reads it again comes to MyThread::startThread().
    I couldn't figure out the cause

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qthread n QTimer Problem

    I would do it like this:
    Qt Code:
    1. void MyThread::run()
    2. {
    3. int fd0 = open("/home/prince/mydev01",O_RDWR);
    4. ...
    5. while( ! _end ) {
    6. read(fd0,&x,sizeof(int));
    7. ...
    8. _semaphore.acquire();
    9. }
    10. close(fd0);
    11. }
    12.  
    13. void MyThread::startThread()
    14. {
    15. if( isRunning() ) {
    16. _semaphore.release();
    17. }
    18. else {
    19. start( LowPriority );
    20. }
    21. }
    22.  
    23. void MyThread::stopThread()
    24. {
    25. _end = true;
    26. _semaphore.release();
    27. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Apr 2006
    Posts
    122
    Thanks
    18
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Qthread n QTimer Problem

    thanks jacek! i am trying this code. My purpose is while reading from fifo gui shouldn't be blocked and it keep o updating as well. That ws the reason i was reading elements from fifo one at a time. Will not this code will block event loop.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qthread n QTimer Problem

    Quote Originally Posted by quickNitin
    thanks jacek! i am trying this code. My purpose is while reading from fifo gui shouldn't be blocked and it keep o updating as well. [...] Will not this code will block event loop.
    It won't block the event loop, because it will happen in a different thread.

    Quote Originally Posted by quickNitin
    That ws the reason i was reading elements from fifo one at a time.
    In that case maybe you don't need that timer at all? Maybe a QThread::sleep() will be enough? How often that device will produce data?

  5. #5
    Join Date
    Apr 2006
    Posts
    122
    Thanks
    18
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Qthread n QTimer Problem

    an algo will write points to fifo. Appx every second

  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: Qthread n QTimer Problem

    If the thread only reads data, maybe it's enough to do a blocking (temporary) read in the other thread without any need for other sync mechanisms. "waitForReadyRead" may be helpfull here.

Similar Threads

  1. Is it possible to create a QThread without inheriting ?
    By probine in forum Qt Programming
    Replies: 6
    Last Post: 23rd March 2006, 22:51
  2. Replies: 6
    Last Post: 17th March 2006, 17:48
  3. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  4. Replies: 16
    Last Post: 7th March 2006, 15:57
  5. Compilation problem, don't know why
    By yellowmat in forum Newbie
    Replies: 6
    Last Post: 2nd March 2006, 15:36

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.