Nominate our 2022 Qt Champions!

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

  • 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.

  • 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.

  • 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!

    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;

Log in to reply