[Qt-interest] Basic QThread question
Sean Harmer
sean.harmer at maps-technology.com
Mon May 17 10:30:27 CEST 2010
Hi Phil,
On Saturday 15 May 2010 08:23:13 Phil wrote:
> On Thu, 13 May 2010 10:15:28 pm Sean Harmer wrote:
> > Hi Phil,
> >
> > On Thursday 13 May 2010 11:17:51 Phil wrote:
> > > On Fri, 7 May 2010 06:14:02 pm Sean Harmer wrote:
> > Does that help? If not can you specify the desired flow of logic more
> > precisely?
>
> Yes it does Sean, however, I'm hung up on something that is probably basic
> C++ rather than a logic error. At the risk of being jumped on, here is the
> problem. After getting nowhere for most of the past two days I now realise
> that I have two instances of classA. Once this is sorted out I should be
> able to get the flow of the programme correct.
>
> SelectItem is a slot that is called when an item is selected from a drop
> down list on the main window.
>
> void Pest::selectItem(const QString &listItem)
> {
> emit stop(); //stop the poller - this may not be necessary
>
> Class_A classA; //this type is defined in the Pest header file
>
> // Class_A's private attributes are initialised with valid data as
> follows
>
> classA.getInitialValues(QString, double, double, etc);
>
> emit start(); //restart the poller
> }
According to the above you are creating an instance of Class_A on the stack
and it will be destroyed at the end of the Pest::selectItem() function. Is
that really the case or do you mean that classA is a member variable of the
Pest class?
> void DevicePoller::poll()
> {
> // Talk to your device here. Since I do not have a device
> // I will simply generate a psuedo-random number
> int x = qrand() % 10;
>
> // Let the world know that we have a new value
> emit value( x );
>
> QStringList list = classA.getData();
>
> //classA is not the same instance as the classA type in the Pest class
> above which means that list does not contain valid data. How do I organise
> the same instance across two classes?
>
> emit dataReady(list);
> }
>
> Maybe there is a more logical method to share data between classes.
The usual way to share objects between threads is to use a pointer or a
reference. So you could add a Class_A* member to the DevicePoller class and
set this before you start the poller.
You need to be slightly careful though because if you are accessing an object
from more than one thread you then introduce the possibility of race
conditions etc. If you have the source for Class_A then you will be best to
protect the data members of Class_A from simultaneous access from multiple
threads by using a QMutex or similar. Depending upon how complex Class_A is
this could get complicated.
A much easier way is to try to avoid having multiple threads access Class_A at
all. For example what you could do is call Class_A::getData() in a slot that
runs in your main thread in response to the value(int) signal being emitted by
your polling thread. That way you only need the single instance of Class_A -
the one in the main thread.
It is difficult to say which approach is better for you without knowing what
Class_A actually does and how long the Class_A::getIntitialValues() and
getData() functions take. If possible I would go with the second option as it
removes the possibility of concurrency issues within Class_A.
> Any help provided will, as always, be greatly appreciated.
No problem. If you still run into trouble, come back with more details on
Class_A.
Good luck,
Sean
More information about the Qt-interest-old
mailing list