Results 1 to 2 of 2

Thread: Sharing Data Between two Threads - QQueue or Semaphores?

  1. #1
    Join Date
    Nov 2020
    Qt products
    MacOS X Windows

    Default Sharing Data Between two Threads - QQueue or Semaphores?

    I have a thread that is explicitly focused on monitoring for events--Producer Thread. The thread runs a loop which checks for an event to happen in hardware. When an event is triggered, an integer is generated. The event needs to be pushed to a second, Worker, thread. Note: Events can be triggered within microseconds of each other.

    The worker thread needs to run some analytics on each triggered event and display it to the Gui.

    Event Variable Question:
    Based on the above, my question is on the correct variable type to use for the events variable a QQueue or Buffer/Semaphore combination?

    1. The Producer Thread should have minimal to no delays to avoid missing an event
    2. The events shall be processed and not overwritten by a second event

    My Understanding:
    As I understand it, in this instance of thread use, a mutex would be needed for use of the QQueue which could possibly create some delay in the producer thread writing an event to the Queue. --Please confirm this as well.

    Thanks for the help.

  2. #2
    Join Date
    Jan 2008
    Alameda, CA, USA
    Thanked 776 Times in 766 Posts
    Qt products
    Windows Android

    Default Re: Sharing Data Between two Threads - QQueue or Semaphores?

    IMO, there is no need for a shared variable. Use a signal-slot connection of the Qt::QueuedConnection type (the default for inter-thread connections) and pass the integer that way. The slot will execute the signals in the order received so there should be no issues with data being lost or need for locking mechanisms.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    Knudsen09 (10th November 2020)

Similar Threads

  1. Data sharing between threads
    By havij000 in forum Qt Programming
    Replies: 28
    Last Post: 14th August 2013, 09:39
  2. Communication/data sharing between threads
    By Tottish in forum Newbie
    Replies: 6
    Last Post: 8th July 2013, 07:33
  3. Sharing between two Threads
    By donglebob in forum General Programming
    Replies: 2
    Last Post: 30th October 2008, 09:58
  4. Sharing data between threads
    By bbui210 in forum Qt Programming
    Replies: 15
    Last Post: 19th October 2008, 18:56
  5. Sharing data across threads
    By jphn_crichton in forum Qt Programming
    Replies: 11
    Last Post: 5th May 2008, 19:29

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.