Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How can multiple threads have the same thread ID?



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



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



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


  • Lifetime Qt Champion

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



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



  • @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();
    }
    


  • @KroMignon I will fix it thank you


Log in to reply