Synchronization (computer science): Difference between revisions

Content deleted Content added
Line 76:
 
==Support in programming languages==
In [[Java (programming language)|Java]], one way to prevent thread interference and memory consistency errors, is by wrapping a block of code in a ''synchronized(someObject){...}'' section. This forces any thread to acquire the lock of the object represented by ''someObject'' before it can execute the block. The lock is automatically released when the thread which acquired the lock, and is then executing the block, leaves the block or enters the waiting state within the block. Any variable updates, made by a thread in a synchronized block, become visible to other threads when they similarly acquire the lock and execute the block. Additionally, when a method signature is prefixed with the ''synchronized'' keyword, the lock of the declaring object is used. Using these two implementations, any object may be used to provide the lock.
 
Java ''synchronized'' blocks, in addition to enabling mutual exclusion and memory consistency, enable signaling—i.e., sending events from threads which have acquired the lock and are executing the code block to those which are waiting for the lock within the block. This means that Java ''synchronized'' sections therefore combine the functionality of [[Lock (computer science)|mutexes]] and [[Event (synchronization primitive)|events]]. Such primitivea construct is known as a [[Monitor (synchronization)|synchronization monitor]].
 
Any object may be used as a lock/monitor in Java. The declaring object is a lock object when the whole method is marked with ''synchronized''.
 
The [[.NET Framework]] has synchronization primitives. "Synchronization is designed to be cooperative, demanding that every thread or process follow the synchronization mechanism before accessing protected resources (critical section) for consistent results." In .NET, locking, signaling, lightweight synchronization types, spinwait and interlocked operations are some of mechanisms related to synchronization.<ref>{{cite web|title=Synchronization Primitives in .NET framework|url=http://msdn.microsoft.com/en-us/library/ms228964%28v=vs.110%29.aspx|website=MSDN, The Microsoft Developer Network|publisher=Microsoft|access-date=23 November 2014}}</ref>