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. QTConcurent and lambda in QT6
Forum Updated to NodeBB v4.3 + New Features

QTConcurent and lambda in QT6

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 4 Posters 2.3k 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.
  • J Offline
    J Offline
    JulsPower
    wrote on last edited by
    #7

    Now for my other question which is more to educate me than fix something

    when I tried in my main thread to use db.open().then [] () function to get the return of the open function at a later point.
    I get the correct return value, but I tried to show a QMessageBox::critical in case of error
    it shows but the program crash in sigsev shortly after.

    why is that? the then function is executed in the main thread right? not by the database qthreadpool one?

    JonBJ 1 Reply Last reply
    0
    • Christian EhrlicherC Christian Ehrlicher

      This usecase is weird - use a dedicated QThread then. As soon as someone else increases the thread count you will get into big trouble.

      J Offline
      J Offline
      JulsPower
      wrote on last edited by
      #8

      @Christian-Ehrlicher said in QTConcurent and lambda in QT6:

      This usecase is weird - use a dedicated QThread then. As soon as someone else increases the thread count you will get into big trouble.

      ill take that into consideration for sure I find how with he used qthreadpool instead of qthread

      I don't see how someone could increase the threadcount those its limited to 1 by: m_pool.setMaxThreadCount(1);

      Christian EhrlicherC 1 Reply Last reply
      0
      • J JulsPower

        Now for my other question which is more to educate me than fix something

        when I tried in my main thread to use db.open().then [] () function to get the return of the open function at a later point.
        I get the correct return value, but I tried to show a QMessageBox::critical in case of error
        it shows but the program crash in sigsev shortly after.

        why is that? the then function is executed in the main thread right? not by the database qthreadpool one?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #9

        @JulsPower said in QTConcurent and lambda in QT6:

        it shows but the program crash in sigsev shortly after.

        Run under a debugger or show your code. And make sure you call no UI code from your secondary thread.

        J 1 Reply Last reply
        1
        • J JulsPower

          @Christian-Ehrlicher said in QTConcurent and lambda in QT6:

          This usecase is weird - use a dedicated QThread then. As soon as someone else increases the thread count you will get into big trouble.

          ill take that into consideration for sure I find how with he used qthreadpool instead of qthread

          I don't see how someone could increase the threadcount those its limited to 1 by: m_pool.setMaxThreadCount(1);

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by Christian Ehrlicher
          #10

          @JulsPower said in QTConcurent and lambda in QT6:

          I don't see how someone could increase the threadcount those its limited to 1 by: m_pool.setMaxThreadCount(1);

          Another developer who is modifying your code and don't know about such strange constraints for instance.

          I get the correct return value, but I tried to show a QMessageBox::critical in case of error
          it shows but the program crash in sigsev shortly after.

          You must not show an ui element from outside the main (gui) thread.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          J 1 Reply Last reply
          1
          • Christian EhrlicherC Christian Ehrlicher

            @JulsPower said in QTConcurent and lambda in QT6:

            I don't see how someone could increase the threadcount those its limited to 1 by: m_pool.setMaxThreadCount(1);

            Another developer who is modifying your code and don't know about such strange constraints for instance.

            I get the correct return value, but I tried to show a QMessageBox::critical in case of error
            it shows but the program crash in sigsev shortly after.

            You must not show an ui element from outside the main (gui) thread.

            J Offline
            J Offline
            JulsPower
            wrote on last edited by
            #11

            @Christian-Ehrlicher said in QTConcurent and lambda in QT6:

            You must not show an ui element from outside the main (gui) thread.

            But the then function is run in the main thread that own the gui that shouldn't be the problem.

            Christian EhrlicherC 1 Reply Last reply
            0
            • J JulsPower

              @Christian-Ehrlicher said in QTConcurent and lambda in QT6:

              You must not show an ui element from outside the main (gui) thread.

              But the then function is run in the main thread that own the gui that shouldn't be the problem.

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #12

              @JulsPower said in QTConcurent and lambda in QT6:

              But the then function is run in the main thread that own the gui that shouldn't be the problem.

              You a) did not show us code and b) did not do what @JonB suggested so how should we help?

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              2
              • JonBJ JonB

                @JulsPower said in QTConcurent and lambda in QT6:

                it shows but the program crash in sigsev shortly after.

                Run under a debugger or show your code. And make sure you call no UI code from your secondary thread.

                J Offline
                J Offline
                JulsPower
                wrote on last edited by
                #13

                @JonB said in QTConcurent and lambda in QT6:

                @JulsPower said in QTConcurent and lambda in QT6:

                it shows but the program crash in sigsev shortly after.

                Run under a debugger or show your code. And make sure you call no UI code from your secondary thread.

                here is the code that open the db in the main thread (which call the thread to open the db in fact)

                    m_jdb->open().then([&](const bool &b){
                        if(!b)
                            QMessageBox::critical(this, "Error", "Couldn't connect to DB");
                    });
                

                80059239-e36d-4bd0-9f51-1488402eca2d-image.png
                the debugger doesn't help me much here it look like a gui bug but if I breakpoint into the then function "this" is the mainwindow pointer

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  JulsPower
                  wrote on last edited by
                  #14

                  Here most of the related code
                  jdatabase.h

                  #ifndef JDATABASE_H
                  #define JDATABASE_H
                  #include <QThreadPool>
                  #include <QFuture>
                  #include <QSqlDatabase>
                  #include <QtConcurrent>
                  
                  
                  class JDatabase : public QObject
                  {
                      Q_OBJECT
                  
                  signals:
                      void Connected();
                      void FailToConnect();
                  public:
                      JDatabase(QWidget *parent = nullptr);
                      ~JDatabase();
                      QFuture<bool> open();
                  
                  private:
                      QThreadPool m_pool;
                  };
                  
                  #endif // JDATABASE_H
                  

                  jdatabase.cpp

                  #include "jdatabase.h"
                  
                  JDatabase::JDatabase(QWidget *parent)
                  {
                      // limit to one thread
                      m_pool.setMaxThreadCount(1);
                      // prevent automatic deletion and recreation
                      m_pool.setExpiryTimeout(-1);
                  }
                  
                  JDatabase::~JDatabase()
                  {
                  //    m_pool.deleteLater();
                  }
                  
                  QFuture<bool> JDatabase::open()
                  {
                  
                      //m_pool,
                      return QtConcurrent::run(&m_pool, [&]()
                      {
                          QString connectString = "Driver={SQL Server};Server=192.168.21.174;Database=DBMachines;";
                  
                          auto db = QSqlDatabase::addDatabase("QODBC");
                          db.setUserName("Julien");
                          db.setPassword("MB6262");
                          db.setDatabaseName(connectString);
                          auto s= db.connectOptions();
                          db.open();
                          if(db.isOpen())
                              emit Connected();
                          else
                              emit FailToConnect();
                          return db.isOpen();
                      });
                  }
                  

                  mainwindow.h declaration

                      JDatabase *m_jdb;
                  

                  and in mainwindow.cpp

                      //Connection base de donnée
                      m_jdb = new JDatabase(this);
                      connect(m_jdb, &JDatabase::Connected, this, [&](){QMessageBox::critical(this, "YAY", "Connecté, refresh"); RefreshDB();});
                  //    connect(m_jdb, &JDatabase::FailToConnect, this, [&](){QMessageBox::critical(this, "Erreur", "Impossible de ce connecter à la base de donnée");});
                      m_jdb->open().then([&](const bool &b){
                          if(!b)
                              QMessageBox::critical(this, "Error", "Couldn't connect to DB");
                      });
                  

                  the signal work as intended but I also tought that the then function would work

                  thanks guys

                  Christian EhrlicherC 1 Reply Last reply
                  0
                  • J JulsPower

                    Here most of the related code
                    jdatabase.h

                    #ifndef JDATABASE_H
                    #define JDATABASE_H
                    #include <QThreadPool>
                    #include <QFuture>
                    #include <QSqlDatabase>
                    #include <QtConcurrent>
                    
                    
                    class JDatabase : public QObject
                    {
                        Q_OBJECT
                    
                    signals:
                        void Connected();
                        void FailToConnect();
                    public:
                        JDatabase(QWidget *parent = nullptr);
                        ~JDatabase();
                        QFuture<bool> open();
                    
                    private:
                        QThreadPool m_pool;
                    };
                    
                    #endif // JDATABASE_H
                    

                    jdatabase.cpp

                    #include "jdatabase.h"
                    
                    JDatabase::JDatabase(QWidget *parent)
                    {
                        // limit to one thread
                        m_pool.setMaxThreadCount(1);
                        // prevent automatic deletion and recreation
                        m_pool.setExpiryTimeout(-1);
                    }
                    
                    JDatabase::~JDatabase()
                    {
                    //    m_pool.deleteLater();
                    }
                    
                    QFuture<bool> JDatabase::open()
                    {
                    
                        //m_pool,
                        return QtConcurrent::run(&m_pool, [&]()
                        {
                            QString connectString = "Driver={SQL Server};Server=192.168.21.174;Database=DBMachines;";
                    
                            auto db = QSqlDatabase::addDatabase("QODBC");
                            db.setUserName("Julien");
                            db.setPassword("MB6262");
                            db.setDatabaseName(connectString);
                            auto s= db.connectOptions();
                            db.open();
                            if(db.isOpen())
                                emit Connected();
                            else
                                emit FailToConnect();
                            return db.isOpen();
                        });
                    }
                    

                    mainwindow.h declaration

                        JDatabase *m_jdb;
                    

                    and in mainwindow.cpp

                        //Connection base de donnée
                        m_jdb = new JDatabase(this);
                        connect(m_jdb, &JDatabase::Connected, this, [&](){QMessageBox::critical(this, "YAY", "Connecté, refresh"); RefreshDB();});
                    //    connect(m_jdb, &JDatabase::FailToConnect, this, [&](){QMessageBox::critical(this, "Erreur", "Impossible de ce connecter à la base de donnée");});
                        m_jdb->open().then([&](const bool &b){
                            if(!b)
                                QMessageBox::critical(this, "Error", "Couldn't connect to DB");
                        });
                    

                    the signal work as intended but I also tought that the then function would work

                    thanks guys

                    Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #15

                    Take a look at the complete backtrace - I'm pretty sure it's not executed in the main thread as I don't see how this should be achieved.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    J 1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      Take a look at the complete backtrace - I'm pretty sure it's not executed in the main thread as I don't see how this should be achieved.

                      J Offline
                      J Offline
                      JulsPower
                      wrote on last edited by
                      #16

                      @Christian-Ehrlicher said in QTConcurent and lambda in QT6:

                      Take a look at the complete backtrace - I'm pretty sure it's not executed in the main thread as I don't see how this should be achieved.

                      the breakpoint in the then lambda give me this
                      6fc3e6e3-4991-419d-8ada-88999763d3fb-image.png

                      then I run the app the msgbox shows
                      and as soon my pointer the into the box I get sigsev
                      4375a312-f5b9-4b05-b29e-125b08658306-image.png

                      how could I find if its the right thread showing the msgbox?, did I understand the then function correctly?

                      Christian EhrlicherC 1 Reply Last reply
                      0
                      • J JulsPower

                        @Christian-Ehrlicher said in QTConcurent and lambda in QT6:

                        Take a look at the complete backtrace - I'm pretty sure it's not executed in the main thread as I don't see how this should be achieved.

                        the breakpoint in the then lambda give me this
                        6fc3e6e3-4991-419d-8ada-88999763d3fb-image.png

                        then I run the app the msgbox shows
                        and as soon my pointer the into the box I get sigsev
                        4375a312-f5b9-4b05-b29e-125b08658306-image.png

                        how could I find if its the right thread showing the msgbox?, did I understand the then function correctly?

                        Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #17

                        Take a breakpoint on 'b' and look at the backtrace to see if it is called from main() and/or check the currentThreadId() with the main .thread id (qApp->threadId())

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        J 1 Reply Last reply
                        0
                        • Christian EhrlicherC Christian Ehrlicher

                          Take a breakpoint on 'b' and look at the backtrace to see if it is called from main() and/or check the currentThreadId() with the main .thread id (qApp->threadId())

                          J Offline
                          J Offline
                          JulsPower
                          wrote on last edited by
                          #18

                          @Christian-Ehrlicher said in QTConcurent and lambda in QT6:

                          Take a breakpoint on 'b' and look at the backtrace to see if it is called from main() and/or check the currentThreadId() with the main .thread id (qApp->threadId())

                          HI
                          what is the "backtrace"?
                          here is a breakpoint to b
                          550d6237-72ff-4b4c-84d8-13b6d6706b41-image.png

                          should the then part be executed in the main loop?

                          Christian EhrlicherC 1 Reply Last reply
                          0
                          • J JulsPower

                            @Christian-Ehrlicher said in QTConcurent and lambda in QT6:

                            Take a breakpoint on 'b' and look at the backtrace to see if it is called from main() and/or check the currentThreadId() with the main .thread id (qApp->threadId())

                            HI
                            what is the "backtrace"?
                            here is a breakpoint to b
                            550d6237-72ff-4b4c-84d8-13b6d6706b41-image.png

                            should the then part be executed in the main loop?

                            Christian EhrlicherC Offline
                            Christian EhrlicherC Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by
                            #19

                            So as I already said in all of my previous post the message box is not drawn in the main thread. Fix it by e.g. using signals and slots or don't use threads at all.

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            J 1 Reply Last reply
                            1
                            • Christian EhrlicherC Christian Ehrlicher

                              So as I already said in all of my previous post the message box is not drawn in the main thread. Fix it by e.g. using signals and slots or don't use threads at all.

                              J Offline
                              J Offline
                              JulsPower
                              wrote on last edited by
                              #20

                              @Christian-Ehrlicher said in QTConcurent and lambda in QT6:

                              So as I already said in all of my previous post the message box is not drawn in the main thread. Fix it by e.g. using signals and slots or don't use threads at all.

                              Hi
                              I understand the signal and slot and know how to use them for what I want to do
                              but I wanted to understand better the then function. from what I understood the qfuture would allow to execute code (lambda or a function call) at a later time when the result would have been received. but I don't get why it would be executed by the other thread.

                              thanks again im just trying to understand how thing work (or should work) so I can use those function correctly.

                              Christian EhrlicherC 1 Reply Last reply
                              0
                              • J JulsPower

                                @Christian-Ehrlicher said in QTConcurent and lambda in QT6:

                                So as I already said in all of my previous post the message box is not drawn in the main thread. Fix it by e.g. using signals and slots or don't use threads at all.

                                Hi
                                I understand the signal and slot and know how to use them for what I want to do
                                but I wanted to understand better the then function. from what I understood the qfuture would allow to execute code (lambda or a function call) at a later time when the result would have been received. but I don't get why it would be executed by the other thread.

                                thanks again im just trying to understand how thing work (or should work) so I can use those function correctly.

                                Christian EhrlicherC Offline
                                Christian EhrlicherC Offline
                                Christian Ehrlicher
                                Lifetime Qt Champion
                                wrote on last edited by
                                #21

                                @JulsPower said in QTConcurent and lambda in QT6:

                                but I don't get why it would be executed by the other thread.

                                Because there is no other way - the future is ready and can't simply switch the thread to the main thread (and there is even no reason to do so) so it's executed in the future's thread.

                                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                Visit the Qt Academy at https://academy.qt.io/catalog

                                J 1 Reply Last reply
                                1
                                • Christian EhrlicherC Christian Ehrlicher

                                  @JulsPower said in QTConcurent and lambda in QT6:

                                  but I don't get why it would be executed by the other thread.

                                  Because there is no other way - the future is ready and can't simply switch the thread to the main thread (and there is even no reason to do so) so it's executed in the future's thread.

                                  J Offline
                                  J Offline
                                  JulsPower
                                  wrote on last edited by JulsPower
                                  #22

                                  @Christian-Ehrlicher said in QTConcurent and lambda in QT6:

                                  @JulsPower said in QTConcurent and lambda in QT6:

                                  but I don't get why it would be executed by the other thread.

                                  Because there is no other way - the future is ready and can't simply switch the thread to the main thread (and there is even no reason to do so) so it's executed in the future's thread.

                                  then I cannot see a usage for future then function ...

                                  Christian EhrlicherC 1 Reply Last reply
                                  0
                                  • J JulsPower has marked this topic as solved on
                                  • J JulsPower

                                    @Christian-Ehrlicher said in QTConcurent and lambda in QT6:

                                    @JulsPower said in QTConcurent and lambda in QT6:

                                    but I don't get why it would be executed by the other thread.

                                    Because there is no other way - the future is ready and can't simply switch the thread to the main thread (and there is even no reason to do so) so it's executed in the future's thread.

                                    then I cannot see a usage for future then function ...

                                    Christian EhrlicherC Offline
                                    Christian EhrlicherC Offline
                                    Christian Ehrlicher
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #23

                                    @JulsPower said in QTConcurent and lambda in QT6:

                                    then I cannot see a usage for future then function

                                    The documentation has a nice common use case for it.

                                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                    Visit the Qt Academy at https://academy.qt.io/catalog

                                    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