Mutex: Difference between revisions

added objective-c
((my SBCL doesn't have thread support, so I can't test the code) Undo revision 62770 by Tayloj (Talk))
(added objective-c)
Line 185:
 
<code>when</code> blocks and <code>Ref.whenResolved</code> return a ''promise'' for the result of the deferred action, so the mutex here waits for the gratuitously complicated increment to complete before becoming available for the next action.
 
=={{header|Objective-C}}==
 
<lang objc>NSLock *m = [[NSLock alloc] init];
 
[m lock]; // locks in blocking mode
 
if ([m tryLock]) { // acquire a lock -- does not block if not acquired
// lock acquired
} else {
else ... (*// already locked, dodoes not block *)
}
[m unlock];</lang>
 
=={{header|OCaml}}==
 
OCaml provides a buildbuilt-in [http://caml.inria.fr/pub/docs/manual-ocaml/libref/Mutex.html Mutex module]. <BR>
It is very simple, there are four functions:
 
<lang ocaml>let m = Mutex.create() in
Mutex.lock m; (* locks in blocking mode *)
 
if (Mutex.try_lock m)
then ... (* did the lock *)
else ... (* already locked, do not block *)
if (Mutex.try_lockunlock m);</lang>
then ... (* did the lock *)
else ... (* already locked, do not block *)
Mutex.unlock m;
 
=={{header|Ruby}}==
Anonymous user