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. How can multiple threads have the same thread ID?
QtWS25 Last Chance

How can multiple threads have the same thread ID?

Scheduled Pinned Locked Moved Unsolved General and Desktop
threadsqt5
7 Posts 3 Posters 1.5k Views
  • 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.
  • D Offline
    D Offline
    deleted286
    wrote on 13 Jan 2021, 13:55 last edited by
    #1

    Hi. I have 2 different threads in my program. I've write it in both threads:

    qDebug() << "im running on gui thread " << GetCurrentThreadId();
    

    It gives me the same id. Shouldn't it have given different ids?

    K 1 Reply Last reply 13 Jan 2021, 13:58
    0
    • D deleted286
      13 Jan 2021, 13:55

      Hi. I have 2 different threads in my program. I've write it in both threads:

      qDebug() << "im running on gui thread " << GetCurrentThreadId();
      

      It gives me the same id. Shouldn't it have given different ids?

      K Offline
      K Offline
      KroMignon
      wrote on 13 Jan 2021, 13:58 last edited by
      #2

      @suslucoder said in How can multiple threads have the same thread ID?:

      Shouldn't it have given different ids?

      Are you really sure the code is executed in different threads?

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      D 1 Reply Last reply 13 Jan 2021, 14:02
      0
      • K KroMignon
        13 Jan 2021, 13:58

        @suslucoder said in How can multiple threads have the same thread ID?:

        Shouldn't it have given different ids?

        Are you really sure the code is executed in different threads?

        D Offline
        D Offline
        deleted286
        wrote on 13 Jan 2021, 14:02 last edited by
        #3

        @KroMignon I'm sure but if it has to be give different id's, i will review it

        myThreadObject = new MyThread();
            myQThread = new QThread();
        
            myThreadObject->moveToThread(myQThread);
        
            connect(this, SIGNAL(startWriting()),myThreadObject, SLOT(writeData()));
            connect(myThreadObject, SIGNAL(writingDone()), this, SLOT(writingDoneByThread()));
        
            // Start the new thread
            myQThread->start();
        }
        

        Because of I moved my thread, is it normal to get same id?

        J K 2 Replies Last reply 13 Jan 2021, 14:03
        0
        • D deleted286
          13 Jan 2021, 14:02

          @KroMignon I'm sure but if it has to be give different id's, i will review it

          myThreadObject = new MyThread();
              myQThread = new QThread();
          
              myThreadObject->moveToThread(myQThread);
          
              connect(this, SIGNAL(startWriting()),myThreadObject, SLOT(writeData()));
              connect(myThreadObject, SIGNAL(writingDone()), this, SLOT(writingDoneByThread()));
          
              // Start the new thread
              myQThread->start();
          }
          

          Because of I moved my thread, is it normal to get same id?

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 13 Jan 2021, 14:03 last edited by
          #4

          @suslucoder Where and when do you check the thread id?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          D 1 Reply Last reply 13 Jan 2021, 14:10
          0
          • D deleted286
            13 Jan 2021, 14:02

            @KroMignon I'm sure but if it has to be give different id's, i will review it

            myThreadObject = new MyThread();
                myQThread = new QThread();
            
                myThreadObject->moveToThread(myQThread);
            
                connect(this, SIGNAL(startWriting()),myThreadObject, SLOT(writeData()));
                connect(myThreadObject, SIGNAL(writingDone()), this, SLOT(writingDoneByThread()));
            
                // Start the new thread
                myQThread->start();
            }
            

            Because of I moved my thread, is it normal to get same id?

            K Offline
            K Offline
            KroMignon
            wrote on 13 Jan 2021, 14:05 last edited by KroMignon
            #5

            @suslucoder said in How can multiple threads have the same thread ID?:

            connect(myThreadObject, SIGNAL(writingDone()), this, SLOT(writingDoneByThread()));

            If the check is done in writingDoneByThread() , this will be executed in the thread used by this, not from emitter thread (in which myThreadObject is running).

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            D 1 Reply Last reply 13 Jan 2021, 14:11
            2
            • J jsulm
              13 Jan 2021, 14:03

              @suslucoder Where and when do you check the thread id?

              D Offline
              D Offline
              deleted286
              wrote on 13 Jan 2021, 14:10 last edited by deleted286
              #6

              @jsulm the first one is main.cpp, which I learned it is the main thread. Like;

              qDebug() << "im running on gui thread " << GetCurrentThreadId();
              

              The second one is here:

              mythread.cpp

              void mythread::writeData()
              {
                  QFile::copy("C:/Users/ilknu/Documents/ThreadExample/read.txt", "C:/Users/ilknu/Documents/ThreadExample/write.txt" );
                  QFile file2("C:/Users/ilknu/Documents/ThreadExample/write.txt");
                  if(file.open(QIODevice::ReadOnly)) {
                      QTextStream in(&file);
                      while (!in.atEnd())
                      {
                          QString line2=in.readLine();
                          QStringList list2 = line2.split(QLatin1Char(' '), Qt::SkipEmptyParts);
                          for(const QString &entry : list2)
                          {
                              double num = entry.toDouble();
                              queue.enqueue(num);
                              qDebug() << num;
                          }
                      }
                  }
                  qDebug() << "Source exists?" << QFile::exists("C:/Users/ilknu/Documents/ThreadExample/deneme.txt");
                  qDebug() << "Destination exists?" << QFile::exists("C:/Users/ilknu/Documents/ThreadExample/write.txt");
                  **qDebug() << "im working in writing thread" << GetCurrentThreadId() ;**
              
                  emit writingDone();
              }
              
              1 Reply Last reply
              0
              • K KroMignon
                13 Jan 2021, 14:05

                @suslucoder said in How can multiple threads have the same thread ID?:

                connect(myThreadObject, SIGNAL(writingDone()), this, SLOT(writingDoneByThread()));

                If the check is done in writingDoneByThread() , this will be executed in the thread used by this, not from emitter thread (in which myThreadObject is running).

                D Offline
                D Offline
                deleted286
                wrote on 13 Jan 2021, 14:11 last edited by
                #7

                @KroMignon I will fix it thank you

                1 Reply Last reply
                0

                5/7

                13 Jan 2021, 14:05

                • Login

                • Login or register to search.
                5 out of 7
                • First post
                  5/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved