Resource management (computing): Difference between revisions

Content deleted Content added
m Reverted edits by 174.44.117.48 (talk) to last version by AnomieBOT
Correct minor grammatical error.
Line 77:
 
=== Unwind protection ===
The most common approach to resource management across languages is to use unwind protection, which is called when execution exits a scope – by execution running off the end of the block, returning from within the block, or an exception being throwthrown. This works for stack-managed resources, and is implemented in many languages, including C#, Common Lisp, Java, Python, Ruby, and Scheme. The main problems with this approach is that the release code (most commonly in a <code>finally</code> clause) may be very distant from the acquisition code (it lacks ''adjacency''), and that the acquisition and release code must always be paired by the caller (it lacks ''encapsulation''). These can be remedied either functionally, by using closures/callbacks/coroutines (Common Lisp, Ruby, Scheme), or by using an object that handles both the acquisition and release, and adding a language construct to call these methods when control enters and exits a scope (C# <code>using</code>, Java <code>try</code>-with-resources, Python <code>with</code>); see below.
 
An alternative, more imperative approach, is to write asynchronous code in [[direct style]]: acquire a resource, and then in the next line have a ''deferred'' release, which is called when the scope is exited – synchronous acquisition followed by asynchronous release. This originated in C++ as the ScopeGuard class, by [[Andrei Alexandrescu]] and Petru Marginean in 2000,