A Lock is used to protect a resource from concurrent access
by multiple threads or processes. This is done
by following the rule that the shared resource is only accessed after the
lock has been obtained. The operating system ensures that only one
thread can have taken a lock at any one time. Once the resource is
no longer required, the lock is released so that other threads can gain access.
The create function is used to create a Lock.
The following creates an un-named lock:
(create Lock)
If this Lock object is shared between threads, it can be used by
those threads to synchronise access to another shared resource.
Named locks can be used to lock a resource across different processes -- this mirrors the named/un-named Event object usage. Locks of the same name are essentially the 'same' lock. A named lock is created as follows:
(create Lock "fred")
Locks are most often used with the LifetimeLock
object, which holds a lock for as long as the lifetime-lock is in existence.
This ensures that the lock is released properly even if an
Error is thrown.
| property name | property type | description |
|---|---|---|
name |
String | the name of the lock |
| method name | description |
|---|---|
lock |
take the lock -- so that no other thread can take it |
unlock |
release the lock -- so that another thread can take it |
isLocked |
test if the lock has been taken |
lock
Take the lock -- so that no other thread can take it. The time (in milliseconds)
to wait for the lock is supplied. If the lock is successfully taken in time,
the Symbol ok is returned, otherwise the
empty List () is returned.
Arguments:
| timeout | Integer |
Return type: Symbol or
the empty List ()
unlock
Release the lock -- so that another thread can take it. If the lock is
successfully released, the Symbol ok
is returned, otherwise the empty List ()
is returned.
Arguments: none
Return type: Symbol or
the empty List ()
isLocked
Test if the lock has been taken. If so, the Symbol ok
is returned, otherwise the empty List ()
is returned.
Arguments: none
Return type: Symbol or
the empty List ()
| Contents | Index | Current topic: objects | Related links: built-in objects |