Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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!


  • Lifetime Qt Champion

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

    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?

    emit a signal and call the next step in the connected slot.



  • Sounds so simple, and if I just return from the slot in case the flag is not set, the pointer will just sit in the main event loop servicing the other slots?


Log in to reply