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. How to send data from one thread to another thread using Signals and slots mechanism?

How to send data from one thread to another thread using Signals and slots mechanism?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.1k 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.
  • A Offline
    A Offline
    activeLearner
    wrote on 18 May 2021, 18:58 last edited by
    #1

    I have searched a lot on the internet about this topic but I could not find a proper solution. So here are my requirements:
    I would be creating two threads - GUI Thread and another worker thread. In the worker thread, I will be using Posix UDP function i.e. "recvFrom()". My intention is to handle the blocking call "recvFrom()" in the worker thread while my GUI thread will be responsive. Everything is fine but my only issue here is to find a way to send data (specifically structure within a structure) to between the threads. Also, I am creating the worker thread that will be a Posix thread but not QThread.

    1. How should I go about creating a Posix Thread but still use only "signal". I hope event loop is not required for worker thread.
    2. If I create a posix thread how can I use "connect" method as the parameters require objects?
    3. Is it a good idea to think about implementing the "data transfer" between threads using PostEvent instead of signals and slots mechanism?
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 18 May 2021, 19:02 last edited by
      #2

      Hi and welcome to devnet,

      Out of curiosity, why not use QUdpSocket and forget about threading since it's asynchronous ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply 19 May 2021, 06:42
      2
      • S SGaist
        18 May 2021, 19:02

        Hi and welcome to devnet,

        Out of curiosity, why not use QUdpSocket and forget about threading since it's asynchronous ?

        A Offline
        A Offline
        activeLearner
        wrote on 19 May 2021, 06:42 last edited by
        #3

        @SGaist I have got a lot of processing to do. So, to avoid the "glitching" effect I am using two threads. Posix UDP because I am forced to use it.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 19 May 2021, 18:50 last edited by
          #4

          In that case why not use a more classic approach of producer/consumer so you simplify your design ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SimonSchroeder
            wrote on 20 May 2021, 07:30 last edited by
            #5

            @activeLearner said in How to send data from one thread to another thread using Signals and slots mechanism?:

            How should I go about creating a Posix Thread but still use only "signal". I hope event loop is not required for worker thread.

            For things to be asynchronous and run in a separate thread you would need the signal/slot connection to be a QueuedConnection (DirectConnection is like a function call (or rather a callback function) and takes place in context (and thus thread) of the caller). A queued connection means that the call to the slot (together with the parameters) is queued in the event loop of the receiver thread.

            So, the short answer to this part of your question: Yes, an event loop is required for the worker thread. Since you have to use Posix threads you should look for other solutions than signals/slots for the communication between the threads.

            Since you already mentioned UDP, you could have a separate Qt thread running which handles UPD communication between the Qt part of your application and the worker thread. Write a class with the slots you need to send data and handles the translation to your UDP communication. From the GUI thread you can then use signals to call the slots of your new class in the separate Qt thread (which should have an event loop).

            1 Reply Last reply
            1

            1/5

            18 May 2021, 18:58

            • Login

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