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. why threads id are same?
Forum Updated to NodeBB v4.3 + New Features

why threads id are same?

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.5k Views 2 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
    iskenderoguz
    wrote on 20 Sept 2015, 14:04 last edited by
    #1

    I create a class to use multi threads

    class ClThread : public QThread
    {
    public:
        ClThread();
        void run();
    signals:
    public slots:
    };
    
    I wrote thread id in constructor
    ClThread::ClThread()
    {
        qDebug()<<"thread : "<<thread()->currentThreadId();
    }
    
    I call it from main class
    
    ClThread *clthread1 = new ClThread();
    ClThread *clthread2 = new ClThread();
    

    Also I tried QRunnable

    class ClThread : public QRunnable
    {
    public:
        ClThread();
        void run();
    signals:
    public slots:
    };
    
    ClThread::ClThread()
    {
        qDebug()<<"thread : "<<QThread::currentThreadId();
    }
    
    ClThread *clthread1 = new ClThread();
    ClThread *clthread2 = new ClThread();
    
    QThreadPool::globalInstance()->start(clthread1);
    QThreadPool::globalInstance()->start(clthread2);
    

    But clthread1 and clthread2 threads id are same always. this is normal ? these are different threads ?

    J 1 Reply Last reply 20 Sept 2015, 14:26
    0
    • I iskenderoguz
      20 Sept 2015, 14:04

      I create a class to use multi threads

      class ClThread : public QThread
      {
      public:
          ClThread();
          void run();
      signals:
      public slots:
      };
      
      I wrote thread id in constructor
      ClThread::ClThread()
      {
          qDebug()<<"thread : "<<thread()->currentThreadId();
      }
      
      I call it from main class
      
      ClThread *clthread1 = new ClThread();
      ClThread *clthread2 = new ClThread();
      

      Also I tried QRunnable

      class ClThread : public QRunnable
      {
      public:
          ClThread();
          void run();
      signals:
      public slots:
      };
      
      ClThread::ClThread()
      {
          qDebug()<<"thread : "<<QThread::currentThreadId();
      }
      
      ClThread *clthread1 = new ClThread();
      ClThread *clthread2 = new ClThread();
      
      QThreadPool::globalInstance()->start(clthread1);
      QThreadPool::globalInstance()->start(clthread2);
      

      But clthread1 and clthread2 threads id are same always. this is normal ? these are different threads ?

      J Offline
      J Offline
      JKSH
      Moderators
      wrote on 20 Sept 2015, 14:26 last edited by
      #2

      @iskenderoguz said:

      I call it from main class
      
      ClThread *clthread1 = new ClThread();
      ClThread *clthread2 = new ClThread();
      

      ...
      But clthread1 and clthread2 threads id are same always. this is normal ?

      Yes, this is normal.

      Your code runs the constructors in the main thread. It does not start any new threads. Think of it this way:

      QFile* file = new QFile("myfile.txt"); // This does NOT create a new file on your hard drive
      QThread* thread = new QThread(); // This does NOT create a new thread in your program
      
      // ===
      
      file->open(QFile::WriteOnly); // This creates a new file on your hard drive
      thread->start(); // This creates a new thread in your program
      

      If you want to start your new thread, you must call QThread::start(). Then, the code inside QThread::run() will start running in a new thread.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      1

      2/2

      20 Sept 2015, 14:26

      • Login

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