Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved How can multiple threads have the same thread ID?

    General and Desktop
    threads qt5
    3
    7
    543
    Loading More Posts
    • 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
      deleted286 last edited by

      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?

      KroMignon 1 Reply Last reply Reply Quote 0
      • KroMignon
        KroMignon @deleted286 last edited by

        @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 Reply Quote 0
        • D
          deleted286 @KroMignon last edited by

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

          jsulm KroMignon 2 Replies Last reply Reply Quote 0
          • jsulm
            jsulm Lifetime Qt Champion @deleted286 last edited by

            @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 Reply Quote 0
            • KroMignon
              KroMignon @deleted286 last edited by 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).

              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 Reply Quote 2
              • D
                deleted286 @jsulm last edited by deleted286

                @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 Reply Quote 0
                • D
                  deleted286 @KroMignon last edited by

                  @KroMignon I will fix it thank you

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post