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. [SOLVED] 2 QThread - Signal could be emitted at the same time?

[SOLVED] 2 QThread - Signal could be emitted at the same time?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.5k Views 2 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.
  • M Offline
    M Offline
    maximus
    wrote on last edited by maximus
    #1

    I have 2 QThread and would like each Thread to emit a signal to a master (my MainWindow).

    This is not a problem since I can connect both Thread to a MainWindow slot. I just want to make sure the logic is good and that it is not possible that both QThread send the signal at the same time?

    My MainWindow will have a QVector and will add item to the QVector with the signals received from the 2 Thread. Exemple of use:
    ex: Thread1 emit signal AddItem(56)
    MainWindow receive signal, add item to QVector if it's not already in it.
    MainWindow emit signal ItemAdded(56)
    Thread1 receive signal and add item to his private QVector

    I'm wondering if the MainWindow slot could be called simultaneously from the threads, since it could be possible the signal is emitted at the exact same time from both Thread.

    The goal of this is too have a centralized QVector in MainWindow class that is the master of all Thread. Threads will ask permission to the MainWindow before adding an item into their own QVector to prevent duplication between threads, if the MainWindow QVector does not have the item, it will reply with a signal saying you can add this item.

    I would really like to resolve this problem with QThread signal/slots mechanism instead of Mutex.


    Free Indoor Cycling Software - https://maximumtrainer.com

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Hi,

      If you use standard connection (Without specify connection type), MainWindow executes one slot at time.
      The problem is how to avoid that signal emitted by MainWindow are caught by both threads.
      I suggest to generate a request UUID for each signal emitted by thread ans use it in signal emitted as response.

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      1
      • M Offline
        M Offline
        maximus
        wrote on last edited by maximus
        #3

        Thanks for confirming this, just wanted to make sure! I think the "connect" use Queued connection by default, that should be good to avoid Thread clashing (Accessing same variable at same time)

        To send the signal from MainWindow to the QThread, I have a ID for each thread, so all thread will receive the message but if the ID is not his ID, the Thread will just ignore the message. In fact I have more than 2 Thread it was just an example.

        Thank you!
        Max

        In case this can help someone, here is the code

        void WorkoutDialog::addToControlList(int antID, int fromHubNumber) {
        
        qDebug() << "Asking Workout Dialog for control" << "antID" << antID << "fromHubNumber" << fromHubNumber << "nbRefuse" << nbRefuse;
        
        if (!hashControlList.contains(antID)) {
            qDebug() << "Ok not in my List, you can control it!" << fromHubNumber;
            hashControlList.insert(antID, fromHubNumber);
            //emit signal back to Hub to let him add this ID
            emit permissionGrantedControl(antID, fromHubNumber);
        }
        else {
            qDebug() << "Sorry this trainer is already being controlled" <<  antId;
        }
        

        Free Indoor Cycling Software - https://maximumtrainer.com

        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