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. QThread::currentThread unexpected (at least for me…) behaviour
Forum Updated to NodeBB v4.3 + New Features

QThread::currentThread unexpected (at least for me…) behaviour

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 4.3k Views 1 Watching
  • 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.
  • I Offline
    I Offline
    inopportuno
    wrote on last edited by
    #1

    I was developing a complex application and I needed to know in a handler the id of the current running thread. I'm using Qt library. In QThread there is the static function QThread::currentThread returning a pointer to the current QThread (wooow!). Unfortunately it looks like to act in a different way I was expecting. The subsequent is a minimalistic Qt program using thread (not the original one).

    @#include <QThread>
    #include <QtCore/QCoreApplication>
    #include <iostream>

    class MyThread : public QThread
    {
    public:
    MyThread(QObject * parent = 0)
    :QThread(parent)
    {
    std::cout << "thread addr (ctor) " << this << std::endl;
    }

    void run()
    {
        std::cout << "current thread addr (run) " << QThread::currentThread() << std::endl;
    }
    

    };

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    std::cout << "main thread addr " << qApp->thread() << std::endl;
    MyThread th;
    th.run();
    while (th.isRunning());
    return 0;
    }@

    Running the program I get:

    @main thread addr 009881D0
    thread addr (ctor) 0039F9A8
    current thread addr (run) 009881D0@

    So looks like that the current thread in the run function is the main thread!!! Probably I'm missing something important...Please, someone can help me and call me stupid? The static QThread::currentThread is too much important for what i'm writing...

    PS I'm working with Qt 4.8 on visual studio 2008 in Win7 pc.

    1 Reply Last reply
    0
    • I Offline
      I Offline
      inopportuno
      wrote on last edited by
      #2

      Sorry, I'm STUPID (and i'm happy to say it!). I called the th.run function instead of th.start. Sorry, again!

      1 Reply Last reply
      0
      • A Offline
        A Offline
        AcerExtensa
        wrote on last edited by
        #3

        And waht does QThread::currentThreadId says?
        There is some difference between getCurrentThreadId and getCurrentThread function in Win32.

        God is Real unless explicitly declared as Integer.

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #4

          [quote author="inopportuno" date="1340281215"]So looks like that the current thread in the run function is the main thread![/quote]Of course it is, because you invoke run() from within the main thread.

          If you want to start a thread use start(), which then executes run() in a seperate thread.

          And, as always when it comes to threading in Qt, make sure you've read "Threads, Events and QObjects":http://qt-project.org/wiki/Threads_Events_QObjects first.

          1 Reply Last reply
          0
          • K Offline
            K Offline
            KA51O
            wrote on last edited by
            #5

            Another important read on the correct use of QThreads is "this one":http://qt-project.org/wiki/QThreads_general_usage. Its not as extensive as the one suggested by Lukas Geyer but instead focuses on the (in my opinion) most important aspect related to the use of QThreads, namely the fact that they themself are not a thread but a wrapper around a thread object. And thus should be used this way.

            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