Cancel queued or running future
cancel(F) stops the queued and running futures contained in F. No action is taken for finished futures. Each element of F that is not already in state 'finished' has its State property set to 'finished', and its Error property is set to contain an MException indicating that execution was cancelled.
Run a function several times until a satisfactory result is found. In this case, the array of futures F is cancelled when a result is greater than 0.95.
N = 100; for idx = N:-1:1 F(idx) = parfeval(@rand,1); % Create a random scalar end result = NaN; % No result yet. for idx = 1:N [~, thisResult] = fetchNext(F); if thisResult > 0.95 result = thisResult; % Have all the results needed, so break break; end end % With required result, cancel any remaining futures cancel(F) result