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. Create Processing Thread in Server Class
Forum Updated to NodeBB v4.3 + New Features

Create Processing Thread in Server Class

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 1.4k Views 1 Watching
  • 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.
  • K Offline
    K Offline
    kevins963
    wrote on last edited by
    #1

    I have a problem in my server class where I create a thread which is used for processing incoming data, however, once the thread is running it will block the signals from emitting for my socket.

    Here is a overview of my class:
    @
    Class cUdpServer
    {

    QUdpSocket * mUdpSocket;
    QThread * mProcessingThread;

    Connect()
    {
    State = PROCESSING_DATA;
    Create Processing Thread
    Connect started signal to OnProcessing
    Thread->start()

    Create Udp Socket
    Connect readReady signal to OnNewData
    Bind and Begin Listening
    }

    OnNewData()
    {
    Read Incoming Data
    Insert new data into circular buffer
    }

    OnProcessing()
    {
    while( State == PROCESSING_DATA )
    {
    Check if new data available in circular buffer
    If available begin parsing data
    }
    }

    }
    @
    However, if I start the thread I never receive data because the signal readReady is never emitted, but if I do not start the thread I receive data just fine.

    I did fix the problem by inheriting the QThread class and everything seems to work fine, however, my question is how would I get this to work with using the QThread as a class member? The reason I have the processing in a forever loop is because I constantly receive new data on the socket, is a looped thread the best way to implement the design, or would destroying and restarting the thread at each new processing better?

    Thanks a lot for any insight into this!

    [[Added code markup, Tobias]]

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vezprog
      wrote on last edited by
      #2

      Create a subclass of a QTcpSocket/UdpSocket with no parent, then create a QThread with the main windows as your parent.

      Call moveToThread() on the QTcpsocket/UdpSocket and give it the reference to the QThread.

      Do all your ready read / processing in the QTcpSocket/UdpSocket subclass.

      For example:
      @
      UdpSubclass *data_comm = new UdpSubclass(); // do not inherit a perent, the move to thread will set it.
      // this is your subclass that you will have to create
      // you can connect your signals and slots to communicate between the MainWindow class that this is created in and the tcp subclass.

      // create a slot in the class that catches your readyRead() signal / stateChanged() signal, and connect them in the constructor of the class
      // connect all of your other stuff like state changes or custom slots in the constructor as well

      QThread *data_comm_t = new QThread(this);
      data_comm->moveToThread(data_comm_t);
      data_comm_t->start();
      @

      1 Reply Last reply
      0
      • JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #3

        Hello,

        If you're in the forever-loop, it will never return, so the thread that runs the loop will never be able to do anything else (i.e. it can't emit signals or invoke slots either).

        You don't need a forever-loop. QUdpSocket will emit readyRead() every time new data arrives, so you can just parse the data in a slot, like dvez43 suggested.

        [quote]how would I get this to work with using the QThread as a class member?[/quote]
        I'm not 100% sure about your intended design, but here are some points that might be helpful to you:

        Right now, OnProcessing() is a member function of cUdpServer, so it should run in the thread that cUdpServer lives in

        If you want your cUdpServer to live in a secondary thread, then it can't hold the QThread object -- instead, the whole cUdpServer will need to be passed to the new thread

        If you want your cUdpServer to live in the main thread, then OnProcessing can't be its member function -- instead, you need a separate object to run code in the new thread

        QThread is not a thread; it is a class that manages a thread. It usually lives in the main thread, and lets you control a different thread (e.g. start()/quit())

        Finally, how busy does your main thread get? And how computationally-expensive is OnProcessing()? If neither one does any heavy computation, then you might not need a separate thread at all.

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        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