Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Implementation of thread
Is this is the correct way of implementing/inheriting the thread:
@Class MyThread:public QThread
MyThread (QObject* parent=0);
~ MyThread ();
void socket_error(QAbstractSocket::SocketError error);
void socket_stateChanged(QAbstractSocket::SocketState state);
@MyThread:: MyThread (QObject* parent)
socket = new QUdpSocket(this);
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()));
//// Data is sending to other files which is running in main thread using QInvokeMethod();
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]
Franzk 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.
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.
Franzk 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.
dangelog 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.
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.
goetz 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?
Yup :). I think i need to read it once again.