QtConcurrent and pointer to a class...this may be a simple c++ dereferencing question
-
I have a class and method that is working fine. Now I want to make it concurrent and am having conceptual issue with QtConcurrent as I'm new to that part of Qt.
Working method:
void ConfiguredDevices::updateConfiguredDevicesState() { Device *device; DeviceListIterator iter(_persistedDevices); while (iter.hasNext()) { iter.next(); device = iter.value(); device->checkAvailability(); } }The checkAvailability method tries to connect to the device and updates a property in the class with the availability. This can take some time, especially if it has to wait for a timeout when the device isn't available, so I wanted to make it concurrent. I was following the nice youtube tutorial from "voidrealms" (https://www.youtube.com/watch?v=tvpC8UrPpZ4) and was thinking I could do the following:
void ConfiguredDevices::updateConfiguredDevicesState() { Device *device; DeviceListIterator iter(_persistedDevices); while (iter.hasNext()) { iter.next(); device = iter.value(); QtConcurrent::run(&(*device), &(device->checkAvailability)); } }But, I get an error, "cannot create a non-constant pointer to a member function." Maybe I'm doing something wrong dereferencing the pointer and trying to cast it to a reference.
Am I going down the right path? Is there another threading path I should consider? Thank you in advance for your help!
-
@Daniel-Williams If I have to do this I will try to write as follows:
QtConcurrent::run( (Device*)device, Device::checkAvailability);
@CP71 I changed to that syntax and get a different error: call to non-static member function without and object argument. However, when I changed to QtConcurrent::run((Device*)device, &Device::checkAvailability); I no longer get the error. I'll run the program to test it out.
Thank you.
-
I have a class and method that is working fine. Now I want to make it concurrent and am having conceptual issue with QtConcurrent as I'm new to that part of Qt.
Working method:
void ConfiguredDevices::updateConfiguredDevicesState() { Device *device; DeviceListIterator iter(_persistedDevices); while (iter.hasNext()) { iter.next(); device = iter.value(); device->checkAvailability(); } }The checkAvailability method tries to connect to the device and updates a property in the class with the availability. This can take some time, especially if it has to wait for a timeout when the device isn't available, so I wanted to make it concurrent. I was following the nice youtube tutorial from "voidrealms" (https://www.youtube.com/watch?v=tvpC8UrPpZ4) and was thinking I could do the following:
void ConfiguredDevices::updateConfiguredDevicesState() { Device *device; DeviceListIterator iter(_persistedDevices); while (iter.hasNext()) { iter.next(); device = iter.value(); QtConcurrent::run(&(*device), &(device->checkAvailability)); } }But, I get an error, "cannot create a non-constant pointer to a member function." Maybe I'm doing something wrong dereferencing the pointer and trying to cast it to a reference.
Am I going down the right path? Is there another threading path I should consider? Thank you in advance for your help!
@Daniel-Williams Hi,
when I use QtConcurrent usually use the following syntax:QFuture cur = QtConcurrent::run( (myclass*)this, myclass::FunctionName);
Note: this example is for a function without parameters.
-
@Daniel-Williams Hi,
when I use QtConcurrent usually use the following syntax:QFuture cur = QtConcurrent::run( (myclass*)this, myclass::FunctionName);
Note: this example is for a function without parameters.
@CP71 Thank you.
In this case I have a QList<Device *> _persistedDevices. I want to call the checkAvailability method on each object in a separate thread. The &(*device) seems to be okay as the first parameter. Getting a reference to the object method seems to be what's causing confusion.
-
@CP71 Thank you.
In this case I have a QList<Device *> _persistedDevices. I want to call the checkAvailability method on each object in a separate thread. The &(*device) seems to be okay as the first parameter. Getting a reference to the object method seems to be what's causing confusion.
@Daniel-Williams If I have to do this I will try to write as follows:
QtConcurrent::run( (Device*)device, Device::checkAvailability);
-
@Daniel-Williams If I have to do this I will try to write as follows:
QtConcurrent::run( (Device*)device, Device::checkAvailability);
@CP71 I changed to that syntax and get a different error: call to non-static member function without and object argument. However, when I changed to QtConcurrent::run((Device*)device, &Device::checkAvailability); I no longer get the error. I'll run the program to test it out.
Thank you.