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?

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 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?

    KroMignonK 1 Reply Last reply
    0
    • D deleted286

      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?

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on 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
      0
      • KroMignonK KroMignon

        @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 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?

        jsulmJ KroMignonK 2 Replies Last reply
        0
        • D deleted286

          @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?

          jsulmJ Online
          jsulmJ Online
          jsulm
          Lifetime Qt Champion
          wrote on 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
          0
          • D deleted286

            @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?

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on 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
            2
            • jsulmJ jsulm

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

              D Offline
              D Offline
              deleted286
              wrote on 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
              • KroMignonK KroMignon

                @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 last edited by
                #7

                @KroMignon I will fix it thank you

                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