Resource acquisition is initialization: Difference between revisions

Content deleted Content added
change short desc to a description; not a categorization
m Disambiguating links to Deadlock (link changed to Deadlock (computer science)) using DisamAssist.
Line 106:
==Typical uses==
 
The RAII design is often used for controlling mutex locks in [[thread (computing)#Multithreading|multi-threaded]] applications. In that use, the object releases the lock when destroyed. Without RAII in this scenario the potential for [[Deadlock (computer science)|deadlock]] would be high and the logic to lock the mutex would be far from the logic to unlock it. With RAII, the code that locks the mutex essentially includes the logic that the lock will be released when execution leaves the scope of the RAII object.
 
Another typical example is interacting with files: We could have an object that represents a file that is open for writing, wherein the file is opened in the constructor and closed when execution leaves the object's scope. In both cases, RAII ensures only that the resource in question is released appropriately; care must still be taken to maintain exception safety. If the code modifying the data structure or file is not exception-safe, the mutex could be unlocked or the file closed with the data structure or file corrupted.