Multi-threading with .net framework

3 vues (au cours des 30 derniers jours)
Seyedfarid Ghahari
Seyedfarid Ghahari le 8 Mar 2017
Commenté : Guillaume le 8 Mar 2017
When I run following code, I expect to see mixed printing from both functions. However, they run sequentially not in parallel. How can I change this code to force Matlab prints results from both functions together? Is there anyway without using Parallel computing?
NET.addAssembly(strcat(pwd,'\','NetDocDelegate.dll'));
divDel = NetDocDelegate.delInteger(@Run);
asyncRes{1} = divDel.BeginInvoke(1,[],[]);
if asyncRes{1}.IsCompleted == false
asyncRes{2} = divDel.BeginInvoke(2,[],[]);
end;
function ret = Run(l)
for i=1:100000
display(strcat(num2str(i),'(',num2str(l),')'))
end;
ret = l;
end

Réponses (1)

Guillaume
Guillaume le 8 Mar 2017
Matlab is single threaded. There is no way for both delegate to run simultaneously. You would indeed need the parallel toolbox to do something like that.
  3 commentaires
Seyedfarid Ghahari
Seyedfarid Ghahari le 8 Mar 2017
Modifié(e) : Guillaume le 8 Mar 2017
Indeed, I am trying to call and run a third party in parallel from Matlab (e.g., calling running several external softwares from matlab but not in series). I wrote this code:
NET.addAssembly(strcat(pwd,'\','NetDocDelegate.dll'));
global RunDel
RunDel = NetDocDelegate.delInteger(@Run);
parfor l=1:2;
asyncRes = RunDel.BeginInvoke(l,[],[]);
end;
function ret = Run(l)
global Analyze
ret = Analyze{l}.RunAnalysis();
end
where "Analyze{}" is an object defined to run a specific software. It is an array, because I would like to open and run that software twice. I get "Cannot load an object of class 'delInteger'" error. If I put "RunDel = NetDocDelegate.delInteger(@Run);" inside the parfor loop I get this error: "MATLAB cannot determine whether "NetDocDelegate" refers to a function or variable". Do you have any comment?
Guillaume
Guillaume le 8 Mar 2017
Sorry, I know nothing about the way the parallel toolbox works, so I'll just point you to this post (and the follow-ups) which may or may not help you.

Connectez-vous pour commenter.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by