Implementation of thread
-
wrote on 30 Sept 2011, 09:46 last edited by
Hi All,
Is this is the correct way of implementing/inheriting the thread:@Class MyThread:public QThread
{
Q_OBJECT
public:
MyThread (QObject* parent=0);
~ MyThread ();QUdpSocket* socket;
protected slots:
void socket_connected();
void socket_disconnected();
void socket_error(QAbstractSocket::SocketError error);
void socket_stateChanged(QAbstractSocket::SocketState state);
void socket_readyRead();
void run();}
@Class Implementation:
@MyThread:: MyThread (QObject* parent)
:QThread(parent)
{socket = new QUdpSocket(this);
socket->bind(port);
connect(d->socket, SIGNAL(connected()), this, SLOT(socket_connected()));
connect(d->socket, SIGNAL(disconnected()), this, SLOT(socket_disconnected()));
connect(d->socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(socket_error(QAbstractSocket::SocketError)));
connect(d->socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(socket_stateChanged(QAbstractSocket::SocketState)));
connect(d->socket, SIGNAL(readyRead()), this, SLOT(socket_readyRead()));
}void MyThread::socket_readyRead()
{this->start(QThread::HighestPriority);
}
void MyThread::run()
{while(1)
{
QByteArray datagram;
qint64 size=d->socket->pendingDatagramSize();
if(size>=0)
//continue;
{mutex->lock();
datagram.resize(socket->pendingDatagramSize());
qint64 dataSize=socket->readDatagram(datagram.data(),datagram.size());int nReceive=datagram.size();
//// Data is sending to other files which is running in main thread using QInvokeMethod();
mutex->unlock();
}
}@First of all is this the correct way of implementing threads since Im not writing the exec() in the run() method.Is it really required??. Or what else i need to change in the above code.
1.Is it required thread for QUDPSocket class to receive data.
2.Some time QSocketNotifier: Invalid id 0 type 'read' disabling error is generating.
3.Some time data stop means ready read signal ll generate but thread is not running.
4. Data arriving at 27 msec.
5.After receiving data some processing is carrying out in the separate thread and then it has to plot in the GUI.
6.How can synchronize the data between receiving and processing thread?. Now Im using the same mutex in both the thread to lock and unlock.Thanks all in advance.
[Edit: Please use @-tags for code snippets /Vass]
-
wrote on 30 Sept 2011, 09:56 last edited by
This is not going to have the desired effect. The slots in your QThread inherited object are going to be handled by the main-threads event loop. Carefully read "Threads Events & QObjects":http://developer.qt.nokia.com/wiki/ThreadsEventsQObjects for more elaborate information.
-
wrote on 30 Sept 2011, 10:07 last edited by
Thanks for reply. if I write exec() in the run() method then it ll creates its won event loop right??. How can i assure that data receiving is happening in the separate thread.
-
wrote on 30 Sept 2011, 10:30 last edited by
Hmm, re-reviewing your code suggests it might work. However, do you have any reason to include the thread? It is not required to be able to read data from the socket. I would suggest you get things working without the thread. You can always complicate matters later on if there's a need to do threading here. In any case, use a signal/slot connection to notify listening objects of the new data. It is thread-safe by nature.
Another suggestion is to choose your slot names somewhat differently. Rule of thumb is to name them as you would name any other function. socket_readyRead() could just as well be a signal name. I'd go for readData(), handleSocketError(), onSocketError(), that sorta thing.
-
wrote on 30 Sept 2011, 11:17 last edited by
From a quick glance: you're creating the sockets in the thread you created your MyThread object (that is, the one it's living in), then accessing the sockets in the thread managed by your MyThread object. That's wrong.
-
wrote on 1 Oct 2011, 06:24 last edited by
As I told data reception is very fast it may be less then 27 msec sometime and finally data plotting is done by main thread only so it ll busy in plotting the data but that side socket may generate ready read signal and data is lost, so I thought thread may solve this problem, I might be wrong :).
And if my implementation is wrong can somebody tell the correct way of implementing the thread, means how and where to create MyThread and socket. -
wrote on 1 Oct 2011, 22:44 last edited by
[quote author="suma" date="1317450267"]And if my implementation is wrong can somebody tell the correct way of implementing the thread, means how and where to create MyThread and socket.[/quote]
That's explained in the mentioned wiki article. You did read it already, didn't you?
-
wrote on 3 Oct 2011, 04:17 last edited by
Yup :). I think i need to read it once again.
1/8