PDA

View Full Version : Passing data to a QThread



lynchkp
10th March 2011, 21:24
Hello everyone,

I'm trying my first implementation of a QThread, and I'm trying to pass some data to the thread. It is to be used as a worker thread to keep the GUI responsive, so multiple threads are not going to be created.

Here is the class definition. I'd like to pass in data to the thread using references to prevent a copy from occurring. The thread will be modifying the original data as well.



class ProcessThread : public QThread
{
public:
void run();
void setSettings(qpiv_settings &settings_in);

private:
qpiv_settings settings;
};





void ProcessThread::setSettings(qpiv_settings &settings_in)
{
settings = settings_in;
}


And here is the basic call to start the thread:


ProcessThread *thread = new ProcessThread;
thread->setSettings(settings);
thread->start();


I've done some small tests, and it doesn't appear like if I modify the values within the thread that it carries back to the original calling function. Am I missing something fundamental?

Thanks in advance!!

schnitzel
10th March 2011, 21:32
I don't think you are using the reference correctly.

wysota
10th March 2011, 21:32
You are making a copy in your setSettings call. Passing a reference is not enough if you immediately copy the object. And what you're doing is not thread-safe, prepare to experience random crashes.

lynchkp
10th March 2011, 21:40
Thanks for the quick replies. I've always made copies before when I deal with classes because the data is small. However, the settings variable will end up being very large, so is there a way to just have the reference in the class? I'm sorry if this is a very basic C++ question.

wysota, do you have any short suggestion on a thread-safe implementation? My main thread reads in very large images and has associated settings, so the data resides in the main thread. I'd like to use this second thread to operate on the image data and settings without having to make a second copy. Then, when the secondary thread finishes, I'd like to be able to use the main thread to continue and display the data, etc.

Thanks again!

wysota
10th March 2011, 22:02
Thanks for the quick replies. I've always made copies before when I deal with classes because the data is small. However, the settings variable will end up being very large, so is there a way to just have the reference in the class? I'm sorry if this is a very basic C++ question.
Sure there is. But you don't have a reference but a copy, just look at your class definition.


wysota, do you have any short suggestion on a thread-safe implementation?
A short suggestion is to synchronize access to a shared resources with a mutex or a semaphore.


My main thread reads in very large images and has associated settings, so the data resides in the main thread. I'd like to use this second thread to operate on the image data and settings without having to make a second copy. Then, when the secondary thread finishes, I'd like to be able to use the main thread to continue and display the data, etc.
If your main thread reads large images then your second thread will not help you prevent freezing the user interface.