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