QTConcurent and lambda in QT6
-
Hi all
QT6.4
im trying to build a little app that connected to a sql server asynchronously. based on this blog: https://lnj.gitlab.io/post/async-databases-with-qtsql/
im have made a simple classe like describedhere is my fonction open:
return QtConcurrent::run(m_pool, [&]() { QString connectString = "Driver={SQL Server};Server=192.168.21.174;Database=DBMachines;"; auto db = QSqlDatabase::addDatabase("QODBC"); db.setUserName("5555"); db.setPassword("6666"); db.setDatabaseName(connectString); auto s= db.connectOptions(); db.open(); if(db.isOpen()) emit Connected(); else emit FailToConnect(); });
if I omit the m_pool it does compile and work, but when written like this it give me a compile error: no matching function for call 'STD::tuple<QThreadpool, JDatabase::open()::<lambda()> >::tuple
jdatabase is my class name here
Anyone can help me out so I can understand better?
then I might have a question on then statement from my main thread :)
-
Hi,
Might be a silly question but is m_pool a pointer ?
-
@JulsPower said in QTConcurent and lambda in QT6:
its a QTheadPool declared in the .h (not as a pointer)
But QtConcurrent::run() needs a pointer as shown in the documentation.
But your code inside the lambda is... strange. You must open a database in the same thread as you're using the connection.
-
@Christian-Ehrlicher
I missed that about the pointer it does compile now (sending the reference of m_pool) to run functionand if you read the article I sent youll see that qthreadpool is configured to use only 1 thread that doesn't expire. so it take care of having always the same thread use the connection to the database
-
This usecase is weird - use a dedicated QThread then. As soon as someone else increases the thread count you will get into big trouble.
-
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?
-
@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);
-
@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.
-
@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.
-
@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.
-
@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?
-
@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"); });
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 -
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
-
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.
-
@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
then I run the app the msgbox shows
and as soon my pointer the into the box I get sigsev
how could I find if its the right thread showing the msgbox?, did I understand the then function correctly?
-
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())
-
@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
should the then part be executed in the main loop?
-
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.
-
@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.