timer and Interruptible=Off button callback priority/preemption?

조회 수: 13 (최근 30일)
Al
Al 2011년 6월 28일
댓글: Stefan Karlsson 2015년 9월 18일
I have a periodic timer with a call back in a GUIDE built GUI app with a button w/call back configured with Interruptible=Off (the timer doesn't have this property). I need to insure that neither call back function (button press or timer) preempts each other, in other words, that a call back that has started to execute will run to completion before the other will run. There doesn't appear to be any semaphore building block functions for the normal non-parallel processing Matlab to handle this manually. Is the timer call-back interruptible by the button call-back?

답변 (3개)

Walter Roberson
Walter Roberson 2011년 6월 28일
The answer apparently depends on your MATLAB version. In newer versions (I'm not sure which one it started in), apparently the timers can interrupt between any two source lines, regardless of whether the callback is marked interruptible or not.
This is not something that I have seen discussed much, or documented: I'm going by my memory of a CSSM thread perhaps a year ago.
  댓글 수: 3
Walter Roberson
Walter Roberson 2011년 6월 28일
I did see that posting back then, but I got the bit about "between any two lines of code" from a response to some other thread.
Walter Roberson
Walter Roberson 2011년 11월 17일
Found some official evidence!
http://www.mathworks.com/support/solutions/en/data/1-2QJ375/index.html?solution=1-2QJ375

댓글을 달려면 로그인하십시오.


Daniel Shub
Daniel Shub 2011년 6월 28일
Non-timer graphic callbacks only run when the event queue is flushed. This means as long as you are careful with your timer callbacks to never flush the even queue, your button callback will never interrupt the timer callback.
Preventing the timer callback from interrupting the button callback is harder. Timers run asynchronously (at least since r2010a) and can interrupt any function between any two lines of code (don't ask me what a line of code is and how mex, dll and java fits in since I have asked the Mathworks and they won't tell me). I can think of two ways of preventing the timer callback from interrupting the button callback:
You could also stop the timer while in the button callback and then restart the timer, with the appropriate time shift, when the callback is finished. Figuring out the delay would be a real bear.
I think better would be to add an invisible object to your GUI with a descriptive tag (a toggle would be ideal) whenever the callback is running. If when the timer starts an object with the correct tag exists, then the timer should issue a uiwait.
  댓글 수: 1
Walter Roberson
Walter Roberson 2011년 6월 28일
Adding and deleting the invisible object could be unnecessary overhead. You could, though, add such an object and have the first line of the button callback set() a property value in the object to non-zero, and the last line of that callback set() it to 0; then, in the timer callback, you could waitfor() that particular property to be 0.
I do not know if this will work, as I do not know about whether timer callbacks are more or less blocking threads that would prevent the button callback from proceeding.

댓글을 달려면 로그인하십시오.


Stefan Karlsson
Stefan Karlsson 2015년 9월 18일
Sometimes I get into the problem that I am in a function that is not interuptible.
An ugly work-around that seems to work (often) is spawn a new temporary timer from the function in question, like so. I would not post this except there seems to be no suggestions on how to address your problems. At least I have a something, so please dont point out how VERY bad this is to do:
%from the function that refuses to be interuptible
start(timer('StartDelay',asLongAsItTakes, ...
'TimerFcn', {@(~,~,p) HopefullyInteruptibleFunction(p), SomeExtraDataToSend}, ...
'StopFcn' , @(obj,~) delete(obj)));
  댓글 수: 1
Stefan Karlsson
Stefan Karlsson 2015년 9월 18일
"asLongAsItTakes" - a delay time you set as low as you can, high enough that your function will be interuptible "HopefullyInteruptibleFunction" - the function that just takes over execution, and will be interuptible(maybe)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Code Execution에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by