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. Qt application and Multithreading error.
Forum Updated to NodeBB v4.3 + New Features

Qt application and Multithreading error.

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 179 Views
  • 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.
  • L Offline
    L Offline
    lakshmanGiri
    wrote on last edited by lakshmanGiri
    #1

    Hi, I am working multi-thread. I have a function called initThread(),
    where I am initializing the thread and then I have a thread function
    where I have the logic I want to perform in that thread. Then,
    I pass it to another function. The thing
    is the thread is starting twice on the second click and thrice on the third click and it gets increases as such.

    I will provide a sample code here, Please go through it.

    //someClass.cpp
    
    someClass::someClass(QObject *parent) : QObject(parent)
    {
    	initThread();
    }
    void someClass::initThread()
    {
    	sampleThread = new QThread(this);
        	connect(sampleThread &QThread::finished, this, [](){
            qDebug() << "sampleThread is finished";
        	});
    }
    void someClass::callSampleThreadFunction()
    {
    	sampleThreadFunction();
    }
    void someClass::sampleThreadFunction()
    {
    	connect(sampleThread &QThread::started, this, [](){
            qDebug() << "sampleThread is started";
        	});
    
    	connect(sampleThread &QThread::started, this, [](){
            	// My logic here
    		sampleThread->exit();
        	});
    	connect(sampleThread, &QThread::finished, this, &QThread::deleteLater);
    	sampleThread->start();
    }
    
    // MainWindow.cpp
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
        , class(new someClass)
    {
    
    }
    void MainWindow::on_pushButton_clicked()
    {
    	
        class->callSampleThreadFunction();
    }
    

    And I get output like this...

    // Output
    
    sampleThread is started
    sampleThread is finished
    
    sampleThread is started
    sampleThread is started
    sampleThread is finished
    
    sampleThread is started
    sampleThread is started
    sampleThread is started
    sampleThread is finished
    

    Why I am getting like this, Am I went wrong anywhere?. Thanks in advance.

    1 Reply Last reply
    0
    • KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by
      #2

      @lakshmanGiri said in Qt application and Multithreading error.:

      Hi, I am working multi-thread. I have a function called initThread(),
      where I am initializing the thread and then I have a thread function
      where I have the logic I want to perform in that thread.

      Then, I have a standard function where I call the thread function. The thing
      is the thread is starting twice on the second click and thrice on the third click and it gets increases as such.

      What is a "thread" function and a "standard" function???

      And why do you not simply use QtConcurrent::run() if you want to start a function in another thread?
      Take a look at https://doc.qt.io/qt-5/qtconcurrentrun.html

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      1 Reply Last reply
      2

      • Login

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