I'd like to use objects in parfor in a way that an update of a first objects property causes the update of properties of the second object.
To illustrate my problem, I've created the following two classes:
properties (Access=private)
function obj = class1(value)
function obj = changeValue(obj,newValue)
notify(obj,'UpdateValue');
and
mode (1,1) logical = false
properties (Access=private)
function obj = class2(var)
obj = obj.setChangingProperty(var);
function property1 = getProperty1(obj)
property1 = obj.propertyStruct.Property1.value;
property1 = obj.property1;
function obj = setChangingProperty(obj,var)
obj.property1 = var.value;
addlistener(var,'UpdateValue',@(src,evnt) obj.ChangeListenerProperty(src,evnt,'property1'));
obj.propertyStruct.Property1 = var;
function obj = ChangeListenerProperty(obj,src,~,varargin)
obj.(varargin{1}) = src.value;
function [val1,val2] = aFunction(obj)
val1 = obj.getProperty1();
Now I'd like to use objects created by these classes in parfor. Therefore, class2 has the property mode, which can be set to true (in case the object is used in parfor) or false elsewise. A test script might look like this:
So in the first case, the change of var's property value didn't cause any change in c2 - as far as I'm concerned this is due to the inability to use listeners within parfor. On the opposite, in the second case c2.property1 is changed as I'm using the object that is saved in the propertyStruct. To be able to access the updated value an aditional method getProperty1 is needed which returns the updated property based on the value of the property mode. Just calling obj.property1 returns the old value.
One solution would be to discard the use of listeners within my objects, but I'd like to use them outside of parfor loops as it's much faster.
My problem is, that within class2 I need to use the method setChangingProperty instead of just writing obj.property = var within the constructor to set a property that can be updated. Moreover, in any of the other methods the updated value of property1 can only be accessed by the method getProperty1. Additionally, it should be possible to have multiple properties like property1.
Is there a more elegant way to update properties of one object on change of another one within parfor?