Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Implementation of thread

Implementation of thread

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 3.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    suma
    wrote on last edited by
    #1

    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]

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Franzk
      wrote on last edited by
      #2

      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.

      "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • S Offline
        S Offline
        suma
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • F Offline
          F Offline
          Franzk
          wrote on last edited by
          #4

          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.

          "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dangelog
            wrote on last edited by
            #5

            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.

            Software Engineer
            KDAB (UK) Ltd., a KDAB Group company

            1 Reply Last reply
            0
            • S Offline
              S Offline
              suma
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                [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?

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  suma
                  wrote on last edited by
                  #8

                  Yup :). I think i need to read it once again.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved