フィルターのクリア

onCleanup order of execution

3 ビュー (過去 30 日間)
Eugeny Sosnovsky
Eugeny Sosnovsky 2011 年 3 月 1 日
編集済み: Jacob Lynch August 2018 年 9 月 27 日
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:
  1. 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?
  2. 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.

採用された回答

Bryan White
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 件のコメント
Eugeny Sosnovsky
Eugeny Sosnovsky 2011 年 5 月 12 日
Thank you very much, this is exactly what I needed. I believe you completely, but I was wondering - is there a place, maybe a web resource, or an in-depth manual, where I could look this type of thing up?
Thank you again!
Bryan White
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
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.

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by