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. updating db through QThread (movetothread)

updating db through QThread (movetothread)

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 2.0k 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.
  • R Offline
    R Offline
    RahibeMeryem
    wrote on last edited by
    #1

    Hi,

    I try to utilize QThread for object detection besides the main Gui thread . It is working well. So I decide to seperate mongodb update (remote) part to another thread. Beacause its blocking and took 0,3 - ,0,6 sec due to network distance etc.

    this is main gui widget :

    Widget::Widget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Widget)
    {
        ui->setupUi(this);
        thread = new QThread();
        worker = new Worker();
        worker->moveToThread(thread);
    
    
        mongo = new mongoUpdate();
        mongo->moveToThread(thread_mongo);
    
        thread_mongo->start();
    
        connect(worker, SIGNAL(valueChanged(QString)), ui->label, SLOT(setText(QString)));
        connect(worker, SIGNAL(frameChanged(QImage)), this , SLOT(displayFrame(QImage)));
    //    connect(worker, SIGNAL(liveFrameChanged(QImage)),this , SLOT(liveChanged(QImage)));
    
        connect(worker , SIGNAL(_bigChip_changed(QImage)),this , SLOT(bigChip_update(QImage)));
    
        connect(worker, SIGNAL(workRequested()), thread, SLOT(start()));
        connect(thread, SIGNAL(started()), worker, SLOT(doWork()));
        connect(worker, SIGNAL(finished()), thread, SLOT(quit()), Qt::DirectConnection);
        connect(worker, SIGNAL(workRequested()), thread, SLOT(start()));
    

    So I want to start mongoupdate thread to wait my command for a seperate proecess.

    in mongo.cpp:

    #include "mongoupdate.h"
    #include <unistd.h>
    #include <QThread>
    
    #include "QDebug"
    mongoUpdate::mongoUpdate(QObject *parent) :
    
        QObject(parent)
    {
        //     MONGO STARTS
            static const std::string mongo_uri = "mongodb://faces:faces@1xx.15x.43.84:27017/obj";
        //    const auto uri = mongocxx::uri::k_default_uri;
            const auto uri = mongo_uri;
    
            mongocxx::options::client client_options;
            mongocxx::instance inst{};
            mongocxx::client conn{mongocxx::uri{uri}};
        //    mongocxx::client conn{uri};
    
            auto db = conn["obj"];
            mongocxx::collection coll = db["objects"];
    
            qDebug() << "PARENT   Mongo Connected" << endl;
    
    
    }
    void mongoUpdate::send_mongo(QImage m_qimg)
    {
    
    write mongo some data when called from the main worker
    But here the mongo connection info  above is unknown ? . how can I  utilize above connection information here ?
    
    
    }
    
    
    
    jsulmJ 1 Reply Last reply
    0
    • R RahibeMeryem

      Hi,

      I try to utilize QThread for object detection besides the main Gui thread . It is working well. So I decide to seperate mongodb update (remote) part to another thread. Beacause its blocking and took 0,3 - ,0,6 sec due to network distance etc.

      this is main gui widget :

      Widget::Widget(QWidget *parent) :
          QWidget(parent),
          ui(new Ui::Widget)
      {
          ui->setupUi(this);
          thread = new QThread();
          worker = new Worker();
          worker->moveToThread(thread);
      
      
          mongo = new mongoUpdate();
          mongo->moveToThread(thread_mongo);
      
          thread_mongo->start();
      
          connect(worker, SIGNAL(valueChanged(QString)), ui->label, SLOT(setText(QString)));
          connect(worker, SIGNAL(frameChanged(QImage)), this , SLOT(displayFrame(QImage)));
      //    connect(worker, SIGNAL(liveFrameChanged(QImage)),this , SLOT(liveChanged(QImage)));
      
          connect(worker , SIGNAL(_bigChip_changed(QImage)),this , SLOT(bigChip_update(QImage)));
      
          connect(worker, SIGNAL(workRequested()), thread, SLOT(start()));
          connect(thread, SIGNAL(started()), worker, SLOT(doWork()));
          connect(worker, SIGNAL(finished()), thread, SLOT(quit()), Qt::DirectConnection);
          connect(worker, SIGNAL(workRequested()), thread, SLOT(start()));
      

      So I want to start mongoupdate thread to wait my command for a seperate proecess.

      in mongo.cpp:

      #include "mongoupdate.h"
      #include <unistd.h>
      #include <QThread>
      
      #include "QDebug"
      mongoUpdate::mongoUpdate(QObject *parent) :
      
          QObject(parent)
      {
          //     MONGO STARTS
              static const std::string mongo_uri = "mongodb://faces:faces@1xx.15x.43.84:27017/obj";
          //    const auto uri = mongocxx::uri::k_default_uri;
              const auto uri = mongo_uri;
      
              mongocxx::options::client client_options;
              mongocxx::instance inst{};
              mongocxx::client conn{mongocxx::uri{uri}};
          //    mongocxx::client conn{uri};
      
              auto db = conn["obj"];
              mongocxx::collection coll = db["objects"];
      
              qDebug() << "PARENT   Mongo Connected" << endl;
      
      
      }
      void mongoUpdate::send_mongo(QImage m_qimg)
      {
      
      write mongo some data when called from the main worker
      But here the mongo connection info  above is unknown ? . how can I  utilize above connection information here ?
      
      
      }
      
      
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @RahibeMeryem said in updating db through QThread (movetothread):

      But here the mongo connection info above is unknown

      Do you mean your conn variable you declared in the constructor?
      Yes, it isn't available here because it is local.
      Why don't you declare it as class variable?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • R Offline
        R Offline
        RahibeMeryem
        wrote on last edited by
        #3

        Eh, due to be an java girl, c++ sometimes look alien.

        Bythe way I found a way to make the code working.

        Now the problem is mongodb update thread blocking all GUI even it is running a different thread.

        VRoninV 1 Reply Last reply
        0
        • R RahibeMeryem

          Eh, due to be an java girl, c++ sometimes look alien.

          Bythe way I found a way to make the code working.

          Now the problem is mongodb update thread blocking all GUI even it is running a different thread.

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          @RahibeMeryem said in updating db through QThread (movetothread):

          due to be an java girl

          It shows, you are leaking every single object you create. Welcome to the hell where no garbage collector can save you

          mongo->moveToThread(thread_mongo); what is thread_mongo?

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          R 1 Reply Last reply
          1
          • mrdebugM Offline
            mrdebugM Offline
            mrdebug
            wrote on last edited by
            #5

            Hi. If you need to share your main db connection between threads I suggest this approach:

                            QString SessionName= SessionGet(); 
                            QSqlDatabase ThreadDb= QSqlDatabase::cloneDatabase(MainClassGlidePROServer->db, SessionName); {
                                if (ThreadDb.open()) {
                                        QSqlQuery query(ThreadDb);
                                        //
                            }{
                                ThreadDb= QSqlDatabase();
                                ThreadDb.removeDatabase(SessionName);
                            }
            

            where
            SessionGet(); // something
            MainClassGlidePROServer->db // the connection defined in the main class.

            Need programmers to hire?
            www.labcsp.com
            www.denisgottardello.it
            GMT+1
            Skype: mrdebug

            VRoninV 1 Reply Last reply
            0
            • VRoninV VRonin

              @RahibeMeryem said in updating db through QThread (movetothread):

              due to be an java girl

              It shows, you are leaking every single object you create. Welcome to the hell where no garbage collector can save you

              mongo->moveToThread(thread_mongo); what is thread_mongo?

              R Offline
              R Offline
              RahibeMeryem
              wrote on last edited by
              #6

              @VRonin

              private:
                  Ui::Widget *ui;
              
                  QThread *thread;
                  QThread *thread_mongo;
              
              
                  Worker *worker;
              
                  mongoUpdate *mongo;
              

              you right we were comfortable and coosy with java :)

              1 Reply Last reply
              0
              • mrdebugM mrdebug

                Hi. If you need to share your main db connection between threads I suggest this approach:

                                QString SessionName= SessionGet(); 
                                QSqlDatabase ThreadDb= QSqlDatabase::cloneDatabase(MainClassGlidePROServer->db, SessionName); {
                                    if (ThreadDb.open()) {
                                            QSqlQuery query(ThreadDb);
                                            //
                                }{
                                    ThreadDb= QSqlDatabase();
                                    ThreadDb.removeDatabase(SessionName);
                                }
                

                where
                SessionGet(); // something
                MainClassGlidePROServer->db // the connection defined in the main class.

                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                @mrdebug said in updating db through QThread (movetothread):

                MainClassGlidePROServer->db

                • I think that's still a race condition
                • You should not keep a QSqlDatabase as a class member. http://doc.qt.io/qt-5/qsqldatabase.html :

                Warning: It is highly recommended that you do not keep a copy of the QSqlDatabase around as a member of a class, as this will prevent the instance from being correctly cleaned up on shutdown. If you need to access an existing QSqlDatabase, it should be accessed with database(). If you chose to have a QSqlDatabase member variable, this needs to be deleted before the QCoreApplication instance is deleted, otherwise it may lead to undefined behavior.

                @RahibeMeryem said in updating db through QThread (movetothread):

                QThread *thread_mongo;

                You never assign anything to it so mongo->moveToThread(thread_mongo); should either do nothing or crash your application

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                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