onCleanup order of execution
    조회 수: 9 (최근 30일)
  
       이전 댓글 표시
    
When onCleanup objects are created in a function, they execute their cleanup routines when destroyed. If several onCleanup objects are created in a function, they will all execute their cleanup routines. I have 2 questions about the order of execution:
- Say 2 onCleanup objects are created, in this order: objCleanup1 and objCleanup2. The function they are created in then completes, and the objects are destroyed. I assume that their corresponding cleanup routines will execute in arbitrary order (or at least not necessarily in the order of creation). Is this correct?
- Say both cleanup routines are very long (i.e., several seconds). Can we safely assume that as long as a single cleanup routine is running, the next one will not start (even if their order is, in fact, arbitrary)?
Thank you in advance.
댓글 수: 0
채택된 답변
  Bryan White
    
 2011년 5월 11일
        1. Yes, you are correct: execution order is not guaranteed. I have seen people "force" the ordering of onCleanup object destruction a couple different ways, none of them being particularly pretty.
Example 1: Extra onCleanup object that references the other onCleanup objects.
 oC1 = onCleanup(@doTask1);
 oC2 = onCleanup(@doTask2);
 ...
 oCN = onCleanup(@doTaskN);
 orderedCleanupObj = onCleanup(@()cellfun(@delete, {oC1, oC2, ..., oCN}));
Example 2: One onCleanup object.
 task1 = {@doTask1, task1Arg1, task1Arg2, ..., task1ArgN};
 task2 = {@doTask2, task1Arg2, task2Arg2, ..., task2ArgN};
 ...
 taskN = {@doTaskN, taskNArg1, taskNArg2, ..., taskNArgN};
 oC = onCleanup(@()cellfun(@(c)feval(c{:}), {task1, task2, ..., taskN}));
Example 3: Incremental Addition via Backreferencing.
 oC1 = onCleanup(@doTask1);
 s1.cleanup1 = oC1;
 ...
 oC2 = onCleanup(@doTask2);
 s2  = struct('cleanup1', s1, 'cleanup2', oC2);
 ...
 oCN = onCleanup(@doTaskN);
 sN  = struct('cleanupN-1', sN-1, 'cleanupN', oCN);
2. Yes, the cleanup routines are synchronous: one must finish for the next to start. The only caveat, then, would be a cleanup task that spawns something asynchronous (e.g., starting a TIMER). But generally speaking, yes.
댓글 수: 2
  Bryan White
    
 2011년 5월 12일
				Hi Eugeny,
Besides MATLAB documentation (namely the <http://www.mathworks.com/help/techdoc/ref/oncleanup.html onCleanup> and <http://www.mathworks.com/help/techdoc/ref/handle.delete.html delete> reference pages and the pages they link) the best web resource I can recommend off-hand would be <http://blogs.mathworks.com/loren/2008/03/10/keeping-things-tidy/ Loren's blog on the onCleanup object>, where some discussion in the comments (circa comment numbers 9-14) goes into some detail.
추가 답변 (1개)
  Jacob Lynch August
      
 2018년 9월 27일
        
      편집: Jacob Lynch August
      
 2018년 9월 27일
  
      onCleanup objects can be stored in a cellular vector, and will be performed first to last. You should know the number of total tasks, and the order they should be run. Usually my code requires a first-in-last-out ordering. I would setup the onCleanup tasks like this:
ON_CLEANUP_TASKS    = cell(N,1);
ON_CLEANUP_TASKS{N} = onCleanup(@()TaskN);
% ... some operations and additional tasks
ON_CLEANUP_TASKS{1} = onCleanup(@()Task1);
Even though TaskN was created first, it will be done last.
댓글 수: 0
참고 항목
카테고리
				Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
			
	제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

