Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Lock (computer science)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Language support== {{see also|Barrier (computer science)}} Programming languages vary in their support for synchronization: * [[Ada (programming language)|Ada]] provides protected objects that have visible protected subprograms or entries<ref>{{cite book | author = ISO/IEC 8652:2007 | title = Ada 2005 Reference Manual | chapter = Protected Units and Protected Objects | chapter-url = http://www.adaic.com/standards/1zrm/html/RM-9-4.html | quote = A protected object provides coordinated access to shared data, through calls on its visible protected operations, which can be protected subprograms or protected entries. | access-date = 2010-02-27 }}</ref> as well as rendezvous.<ref>{{cite book | author = ISO/IEC 8652:2007 | title = Ada 2005 Reference Manual | chapter = Example of Tasking and Synchronization | chapter-url = http://www.adaic.com/standards/1zrm/html/RM-9-11.html | access-date = 2010-02-27 }}</ref> * The ISO/IEC [[C (programming language)|C]] standard provides a standard [[mutual exclusion]] (locks) [[application programming interface]] (API) since [[C11 (C standard revision)|C11]]. The current ISO/IEC [[C++]] standard supports [[C++0x#Threading facilities|threading facilities]] since [[C++11]]. The [[OpenMP]] standard is supported by some compilers, and allows [[critical sections]] to be specified using pragmas. The [[POSIX Threads|POSIX pthread]] API provides lock support.<ref>{{cite web | url=http://www.cs.cf.ac.uk/Dave/C/node31.html#SECTION003110000000000000000 | title=Mutual Exclusion Locks | last=Marshall|first=Dave | date=March 1999 | access-date=2008-05-30}}</ref> [[Visual C++]] provides the <code>synchronize</code> attribute of methods to be synchronized, but this is specific to COM objects in the [[Microsoft Windows|Windows]] architecture and [[Visual C++]] compiler.<ref>{{cite web | url=http://msdn.microsoft.com/en-us/library/34d2s8k3(VS.80).aspx | title=Synchronize | publisher=msdn.microsoft.com | access-date=2008-05-30}}</ref> C and C++ can easily access any native operating system locking features. * [[C Sharp (programming language)|C#]] provides the <code>lock</code> keyword on a thread to ensure its exclusive access to a resource. * [[Visual Basic (.NET)]] provides a <code>SyncLock</code> keyword like C#'s <code>lock</code> keyword. * [[Java (programming language)|Java]] provides the keyword <code>synchronized</code> to lock code blocks, [[Method (computer programming)|methods]] or [[Object (computer science)|objects]]<ref>{{cite web | url=http://java.sun.com/docs/books/tutorial/essential/concurrency/sync.html | title=Synchronization | publisher=[[Sun Microsystems]] | access-date=2008-05-30}}</ref> and libraries featuring concurrency-safe data structures. * [[Objective-C]] provides the keyword <code>@synchronized</code><ref>{{cite web | url=https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Multithreading/ThreadSafety/ThreadSafety.html | title=Apple Threading Reference | publisher=Apple, inc | access-date=2009-10-17}}</ref> to put locks on blocks of code and also provides the [[Class (computer programming)|classes]] NSLock,<ref>{{cite web | url=https://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSLock_Class/Reference/Reference.html | title=NSLock Reference | publisher=Apple, inc | access-date=2009-10-17}}</ref> NSRecursiveLock,<ref>{{cite web | url=https://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSRecursiveLock_Class/Reference/Reference.html | title=NSRecursiveLock Reference | publisher=Apple, inc | access-date=2009-10-17}}</ref> and NSConditionLock<ref>{{cite web | url=https://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSConditionLock_Class/Reference/Reference.html | title=NSConditionLock Reference | publisher=Apple, inc | access-date=2009-10-17}}</ref> along with the NSLocking protocol<ref>{{cite web | url=https://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Protocols/NSLocking_Protocol/Reference/Reference.html | title=NSLocking Protocol Reference | publisher=Apple, inc | access-date=2009-10-17}}</ref> for locking as well. * [[PHP]] provides a file-based locking <ref>{{cite web | url=http://php.net/manual/en/function.flock.php | title=flock }}</ref> as well as a <code>Mutex</code> class in the <code>pthreads</code> extension.<ref>{{cite web | url=http://php.net/manual/en/class.mutex.php | title=The Mutex class | access-date=2016-12-29 | archive-date=2017-07-04 | archive-url=https://web.archive.org/web/20170704152552/http://php.net/manual/en/class.mutex.php | url-status=dead }}</ref> * [[Python (programming language)|Python]] provides a low-level [[Mutual exclusion|mutex]] mechanism with a <code>Lock</code> [[Class (computer programming)|class]] from the <code>threading</code> module.<ref>{{cite web | url=http://effbot.org/zone/thread-synchronization.htm | title=Thread Synchronization Mechanisms in Python | last=Lundh | first=Fredrik | date=July 2007 | access-date=2008-05-30 | archive-date=2020-11-01 | archive-url=https://web.archive.org/web/20201101025814/http://effbot.org/zone/thread-synchronization.htm | url-status=dead }}</ref> * The ISO/IEC [[Fortran]] standard (ISO/IEC 1539-1:2010) provides the <code>lock_type</code> derived type in the intrinsic module <code>iso_fortran_env</code> and the <code>lock</code>/<code>unlock</code> statements since [[Fortran#Fortran 2008|Fortran 2008]].<ref>{{cite web | url=https://wg5-fortran.org/N1801-N1850/N1824.pdf | title=Coarrays in the next Fortran Standard | author=John Reid | year=2010 | access-date=2020-02-17}}</ref> * [[Ruby (programming language)|Ruby]] provides a low-level [[Mutual exclusion|mutex]] object and no keyword.<ref>{{cite web | url=https://docs.ruby-lang.org/en/master/Thread/Mutex.html | title=class Thread::Mutex}}</ref> * [[Rust (programming language)|Rust]] provides the <code>Mutex<T></code><ref>{{cite web |title=std::sync::Mutex - Rust |url=https://doc.rust-lang.org/std/sync/struct.Mutex.html |website=doc.rust-lang.org |access-date=3 November 2020}}</ref> struct.<ref>{{cite web |title=Shared-State Concurrency - The Rust Programming Language |url=https://doc.rust-lang.org/book/ch16-03-shared-state.html |website=doc.rust-lang.org |access-date=3 November 2020}}</ref> * [[x86 assembly language]] provides the <code>LOCK</code> prefix on certain operations to guarantee their atomicity. * [[Haskell]] implements locking via a mutable data structure called an <code>MVar</code>, which can either be empty or contain a value, typically a reference to a resource. A thread that wants to use the resource ‘takes’ the value of the <code>MVar</code>, leaving it empty, and puts it back when it is finished. Attempting to take a resource from an empty <code>MVar</code> results in the thread blocking until the resource is available.<ref name="marlow_conc_haskell">{{cite book|last1=Marlow|first1=Simon|author1-link=Simon Marlow|date=August 2013|title=Parallel and Concurrent Programming in Haskell|url=https://www.oreilly.com/library/view/parallel-and-concurrent/9781449335939/|publisher=[[O’Reilly Media]]|isbn=9781449335946|chapter=Basic concurrency: threads and MVars}}</ref> As an alternative to locking, an implementation of [[software transactional memory]] also exists.<ref>{{cite book|last1=Marlow|first1=Simon|author1-link=Simon Marlow|date=August 2013|title=Parallel and Concurrent Programming in Haskell|url=https://www.oreilly.com/library/view/parallel-and-concurrent/9781449335939/|publisher=[[O’Reilly Media]]|isbn=9781449335946|chapter=Software transactional memory}}</ref> *[[Go (programming language)|Go]] provides a low-level [[Mutual exclusion|Mutex]] object in standard's library [https://pkg.go.dev/sync sync] package.<ref>{{Cite web|title=sync package - sync - pkg.go.dev|url=https://pkg.go.dev/sync#Mutex|access-date=2021-11-23|website=pkg.go.dev}}</ref> It can be used for locking code blocks, [[Method (computer programming)|methods]] or [[Object (computer science)|objects]].
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)