What will happen if a timer object is due start at a calendar time and Matlab is busy?

Viewed 26

What will happen if a timer object is due start at a calendar time and Matlab is busy? Will Matlab execute TimerFcn after the current task is finished or will there be parallel execution or alternating execution?

For example,

t=timer;

t.TimerFcn=@(~,~)disp(clock);

[Y, M, D, H, MI, S] = datevec(now+0.02);

startat(t,Y,M,D,H,MI,S);

what will happen if Matlab is busy in 0.02 days from now?

1 Answers

It looks like MATLAB will pause the currently executing code to run the timer object:

t = timer;
t.TimerFcn = @timer_func;
onesecond = 1/(24*60*60); % days
[Y, M, D, H, MI, S] = datevec(now + 5 * onesecond);
startat(t,Y,M,D,H,MI,S);

disp(datetime('now'))

for ii=1:1000
   if mod(ii,100)==0
      disp('main thread working...')
   end
   a = atan2(rand(1e6,1),rand(1e6,1));
end

function timer_func(~,~)
disp(datetime('now'))
for ii=1:1000
   if mod(ii,100)==0
      disp('timer_func working...')
   end
   a = atan2(rand(1e6,1),rand(1e6,1));
end
end

This outputs:

   10-Aug-2020 23:53:24

main thread working...
main thread working...
   10-Aug-2020 23:53:29

timer_func working...
timer_func working...
timer_func working...
timer_func working...
timer_func working...
timer_func working...
timer_func working...
timer_func working...
timer_func working...
timer_func working...
main thread working...
main thread working...
main thread working...
main thread working...
main thread working...
main thread working...
main thread working...
main thread working...
Related