可能な再注文による保証はありません。 Disposeは、中かっこ(またはあなたの例では暗黙のもの)で呼び出されます。最適化されていないデバッグビルドでは、常に予想される注文が表示されます。 Thread.MemoryBarrier を使用すると、操作の順序。
using (Stuff1 stf1 = new Stuff1(...))//Allocation of stf1
using (Stuff2 stf2 = new Stuff2(...))//Allocation of stf2
{ {
try
{
//... do stuff with stf1 and stf2 here ...
}
catch (Stuff1Exception ex1)
{
//...
}
catch (Stuff2Exception ex2)
{
//...
}
} Thread.MemoryBarrier(); }
Release-mode optimizations are done in such a way as to guarantee predictable results on this thread after all instructions complete. Everything else (view from a different thread on a separate core) is up for grabs. It's not against the rules to have a situation where stf2 is not yet disposed, but stf1 is disposed, as observed from another thread. Unless you force the order of operations. Try writing some asserts and running under Jinx