QThread used to call function pointer
-
Hi guys,
I am developing a user interface which will need to run some code in a dedicated thread. I don't want to use the graphical thread because the source code execution may last few minutes to 20-30 minutes and the user interface will freeze.
So far, I am using a sub class of QThread, redefining the method run and job done. However, I would like to have a generic "Working thread" which is taking a function pointer in the constructor and the run function is simply calling the function pointer.
Here is a simple demo app:
working_thread.h:
working_thread.c:
A dummy object to run some code asynchronously:
But the error I got is the following:
I am a java developer and I can use lambda expression to avoid creating a function attached to an object. I investigated this direction, but each time I got compilation errors.
What am I doing wrong here ?
I thought the
typedef bool (*Runnable)();
declared in the working_thread.h file was generic enough to accept all kind of functions as soon as it has the correct signature.I saw as well the
moveToThread
function, but I'm not sure if it is what I am looking for.Have a nice day,
Pierre-Emmanuel -
@Pierre-Emmanuel said in QThread used to call function pointer:
I thought the typedef bool (*Runnable)(); declared in the working_thread.h file was generic enough to accept all kind of functions as soon as it has the correct signature.
No, it's not. You're trying to use a pointer to a method of a class. Your Runnable is a simple function pointer.
Take a look at https://opensource.com/article/21/2/ccc-method-pointers -
Subclassing Thread is in 99% of all cases the wrong approach. But you're coming from Java so I can't fault you for that :p
What you're looking for is QtConcurrent, it accepts a function pointer and runs that in a different thread from an internal thread pool
https://doc.qt.io/qt-6/qtconcurrent.html#run
make sure your function is truly standalone, no access to shared objects etc
-
Thank you for this information. I am trying now to use
QtConcurrent::run
function but I am facing an issue to be notified after the execution, to get a success status.Here is what I have done:
async_caller.h:
async_caller.c:
main function:
The output is:
I can see that the task is executed in a different thread, but somehow the slot
onAsyncCallComplete
is not called.I watch this youtube tutorial: https://www.youtube.com/watch?v=W9nWyTzxLjs
and added the call to the
deleteLater
slot to be sure the watcher still exists when the callback should be executed.PS @J-Hilk: In Java I would not extends the Thread class but use a function to give to the thread constructor ahah. On a more serious topic, would it be a good idea to create a WorkingThread with a lambda expression as input argument for the constructor ?
Have a nice day guys !
Pierre-Emmanuel -
@Pierre-Emmanuel Please post code as text, not pictures.
You do not have Qt event loop running, that's probably why your slot is not executed: msleep blocks the thread and just after that you terminate the application. -
Hi @jsulm,
I updated the main function to initialize the EventLoop it is working as expected.
Thank you very much for your help,
Pierre-Emmanuel de Robien -