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 - Why do threads wait one another to start running?
Forum Updated to NodeBB v4.3 + New Features

QThread - Why do threads wait one another to start running?

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.7k 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.
  • F Offline
    F Offline
    fnoyanisi
    wrote on last edited by
    #1

    Hi

    I have the following code. When I run the code, first thread 0 starts and finishes and then thread 1 starts and finishes.
    I designed to code such that running time for thread 0 is much longer than thread 1 and I was expecting, while thread 0 is running, I should see thread 1 starts running but it doesn't

    [code]#include <QtCore/QCoreApplication>
    #include <QThread>
    #include <iostream>
    #include <cstdlib>
    #include <windows.h>

    using namespace std;

    static int GlobalThreadId = 0;

    class MyThread : public QThread {
    private:
    int t_id;
    public:
    MyThread();
    void run();
    };

    MyThread::MyThread(){
    cout << "Thread initialized" << endl;
    t_id = GlobalThreadId;
    GlobalThreadId++;
    }

    void MyThread::run (){
    cout << "Thread number " << t_id << " started." << endl;

    int val = (1-t_id)*1000;
    for (int y=0; y<(val*1000); y++)
            for (int l=0; l<100;l++){}
    
    cout << "Thread number " << t_id << " completed." << endl;
    

    }

    int main(int argc, char *argv[])
    {
    MyThread *t[8];
    int i;

    cout << "Number of Threads that will be created : " << QThread::idealThreadCount() << endl;
    
    int CreatedThreadNumber = 0;
    for (int x=0; x < QThread::idealThreadCount() ; x++) {
        t[x] = new MyThread;
        CreatedThreadNumber++;
    }
    
    for (int l=0; l < CreatedThreadNumber; l++) {
        t[l]->run();
    }
    
    cin >> i;
    return 0;
    

    }[/code]

    1 Reply Last reply
    0
    • D Offline
      D Offline
      depecheSoul
      wrote on last edited by
      #2

      Use the start() method to begin execution, not run() method.

      In this link you can find good tutorials about threads: http://www.voidrealms.com/tutorials.aspx?filter=qt

      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