Results 1 to 2 of 2

Thread: Best Way to Poll for a Flag in Main Event Thread

  1. #1
    Join Date
    Dec 2019
    Posts
    32
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Best Way to Poll for a Flag in Main Event Thread

    Hi,

    I know several folks have asked similar questions on this topic but there doesn't seem to be an answer which will work for my situation, at least none that I can find.

    I have multiple threads running, say 10. As each thread runs and when it completes a certain task it emits a signal to the main event thread.
    The slot in the main event thread keeps track of each thread that completes it task. The threads do not die, they just keep working after they send the signal.

    Once all the threads send their signal, the slot sets a flag completed.

    In the main event thread I go through a sequence of processing but can't continue until the flag in the slot shows complete. I don't want to holdup the
    main event thread because it still needs to receive slot information plus other information from other threads unrelated to the ones I'm mentioning.
    Does anyone know how I can poll for the flag without continuing to next phase in the process until the flag is set without holding up the main thread?

    I read about using single shot but not sure how to use that without continuing. I don't want to use a while loop to spin with aQthread ::sleep because
    that will hold up the thread sounds like poor practice. Any help will be appreciated.

    Thanks in advance!

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Best Way to Poll for a Flag in Main Event Thread

    Instead of setting a flag, have the slot emit a master "completed" signal when all of the threads have finished. Connect this signal to some other slot that then processes the composite results of the threads. No polling is needed.

    Of course, you have protected your main thread slot with a mutex or some similar mechanism to ensure that two threads don't attempt to update the status at the same time, right?

    --- Edit

    This last point (on a mutex) may not actually be a requirement. You should probably consult the Qt docs on threading and on signals and slots. In particular, connections between signals and slots across threads should be made as Queued Connections. In that case the slot will be thread safe, since signals will be queued for handling as they are received.
    Last edited by d_stranz; 23rd January 2020 at 18:49.
    <=== 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:

    leinad (23rd January 2020)

Similar Threads

  1. Replies: 1
    Last Post: 10th May 2018, 09:58
  2. Replies: 0
    Last Post: 7th December 2017, 19:33
  3. Replies: 5
    Last Post: 22nd June 2012, 17:40
  4. Replies: 5
    Last Post: 8th February 2012, 10:56
  5. Main Thread Event loop
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 20th March 2007, 13:10

Bookmarks

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.