onCleanup order of execution
8 次查看(过去 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 个评论
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!