PDA

View Full Version : Qthread n QTimer Problem



quickNitin
7th June 2006, 13:56
hi
i am trying to print a no. o points on canvas. These points are read from a fifo through a clas MyThread.


class MyThread: public QThread
{
Q_OBJECT
public:
MyThread(QObject *parent=0);
~MyThread();
double x;
double y;

protected:
void run();
signals:
void dataAvailable();
public slots:
void startThread();

};

#endif

amd its implementation is as


#ifndef MYTHREAD_cpp
#define MYTHREAD_cpp
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "MyThread.h"
#include <iostream.h>


MyThread::MyThread(QObject *parent) :QThread(parent)
{

}

MyThread::~MyThread()
{
}

void MyThread::run()
{
int fd0;


fd0 = open("/home/prince/mydev01",O_RDWR);
if(fd0== -1)
cout<<"\n------------------- No fifo found///////---------------------\n\n";
else
cout<<" Fifo founf \n";

read(fd0,&x,sizeof(int));
read(fd0,&y,sizeof(int));
printf("\nReceived x = %d & y = %d from RTCore\n",
x,y);
close(fd0);
emit dataAvailable();
}
void MyThread::startThread()
{
start(LowPriority);
}


#endif


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

jacek
7th June 2006, 14:49
I would do it like this:
void MyThread::run()
{
int fd0 = open("/home/prince/mydev01",O_RDWR);
...
while( ! _end ) {
read(fd0,&x,sizeof(int));
...
_semaphore.acquire();
}
close(fd0);
}

void MyThread::startThread()
{
if( isRunning() ) {
_semaphore.release();
}
else {
start( LowPriority );
}
}

void MyThread::stopThread()
{
_end = true;
_semaphore.release();
}

quickNitin
8th June 2006, 13:14
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.

jacek
8th June 2006, 14:14
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.


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?

quickNitin
8th June 2006, 15:05
an algo will write points to fifo. Appx every second

wysota
8th June 2006, 15:12
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.