PDA

View Full Version : Pass parameters through QFutureWatcher to finished function



annapCoug
31st January 2018, 03:00
I am trying to move a time consuming part of a function to a concurrent process. I am running into a problem though. There are a lot of parameters passed to this new concurrent function that I need access to after the concurrent processed has finished. Is there a way I can pass certain parameters from the function that setup the QFutureWatcher to the function that is called when finished() fires?

high_flyer
31st January 2018, 10:28
There are a lot of parameters passed to this new concurrent function that I need access to after the concurrent processed has finished.
Why not pack them in a simple data object like a struct?


Is there a way I can pass certain parameters from the function that setup the QFutureWatcher to the function that is called when finished() fires?
At least for me it is not really clear what you are trying to do (not what you are asking about).
Seems like it could be done simpler, and thus maybe avoid the problem you have in the first place.
If you could elaborate a bit more, and with some code, it will help better understand what your needs are.

annapCoug
31st January 2018, 14:22
Here's what is actually happening:

1) User clicks a button to start this process
2) Information is collected from the GUI, a filename is automatically generated
3) Based on the information from the GUI, hardware collects and writes a data file (This is the time consuming process)
4) During the hardware process another process could be launched, data from the GUI could be changed, etc...
5) When the hardware process is complete (finished() fires) I have to process the data file it wrote. I need:
i) The filename of the data it wrote
ii) Information on how the hardware was configured for that particular run (Frequency of a laser, power of the laser, etc...)

Any idea how to launch the concurrent process and then recover the parameters used for that run?

high_flyer
31st January 2018, 14:47
Any idea how to launch the concurrent process and then recover the parameters used for that run?

I fail to see what the problem is.
Synchronizing data between threads (you are talking about a thread here, not a process) is only needed if more than one threads needs to access the data at the "same time".
It doesn't sound like this is the case for you.
Again, I would pack the information in a struct and read from it.

Maybe some code would help to understand better what is the problem you are having.

annapCoug
31st January 2018, 14:59
After the concurrent thread completes, then the parameters used are destroyed right?

The filename created was based on a date/time, the user could have changed values in the GUI and launched a second (Or third, or fourth) thread.

So how do I recover the values used to launch THAT concurrent thread? Even if I stuck them all together in a struct, they would still be deleted when the thread completed.

d_stranz
31st January 2018, 18:01
Even if I stuck them all together in a struct, they would still be deleted when the thread completed.

Why do you think that? If you pack them into a struct, you simply save a copy of that struct in the calling side of your code before you send them off to the thread.

If you don't want to do that, you could customize a QThread instance so that it emits its own finished() signal that includes a copy of the arguments.

Or if you derive from QThread and give your custom QThread class getter / setter methods to retrieve and set the parameters. If you connect a slot to the thread's finished() signal, you can use QObject::sender() in your slot to retrieve the pointer to the QThread that sent the signal and retrieve your parameters through that pointer. The QThread won't be deleted until all slots connected to its signals complete.

There are probably other ways to accomplish your goal; these are just three of the possible solutions.