Qore Programming Language Reference Manual
0.8.11.1
|
A class providing an implementation for a simple thread lock. More...
Public Member Functions | |
constructor () | |
Creates the Mutex object. More... | |
copy () | |
Creates a new Mutex object, not based on the original. More... | |
destructor () | |
Destroys the object. More... | |
nothing | lock () |
Locks the Mutex object; blocks if the lock is already held. More... | |
int | lock (timeout timeout_ms) |
Locks the Mutex object; blocks if the lock is already held. More... | |
int | trylock () |
Acquires the lock only if it is not already held; returns 0 for success (lock acquired) or -1 if the call would block. More... | |
nothing | unlock () |
Unlocks the Mutex object; wakes up one thread if any threads are blocked on this lock. More... | |
Public Member Functions inherited from Qore::Thread::AbstractSmartLock | |
constructor () | |
Throws an exception if called directly; this class can only be instantiated by builtin subclasses. More... | |
string | getName () |
Returns the name of the threading class directly inheriting this class. More... | |
bool | lockOwner () |
Returns True if the calling thread owns the lock, False if not. More... | |
int | lockTID () |
Returns the TID of the thread owning the lock or -1 if the lock is currently not acquired. More... | |
A class providing an implementation for a simple thread lock.
This class inherits AbstractSmartLock, so it can be used by Condition objects.
The Mutex class implements a mutual-exclusion lock for thread locking. Like all Qore thread primitives, objects of this class participate in deadlock detection and throw exceptions when threading errors occur (ex: unlocking a Mutex object locked by another thread, etc). See individual methods for more information on exceptions thrown.
See the AutoLock class for a class that assists in exception-safe Mutex locking.
Additionally, the on_exit statement can provide exception-safe unlocking at the lexical block level for Mutex objects as in the following example:
Qore::Thread::Mutex::constructor | ( | ) |
Creates the Mutex object.
Qore::Thread::Mutex::copy | ( | ) |
Creates a new Mutex object, not based on the original.
Qore::Thread::Mutex::destructor | ( | ) |
Destroys the object.
Note that it is a programming error to delete this object while other threads are blocked on it; in this case an exception is thrown in the deleting thread, and in each thread blocked on this object when it is deleted.
LOCK-ERROR | Object deleted while other threads blocked on it |
nothing Qore::Thread::Mutex::lock | ( | ) |
Locks the Mutex object; blocks if the lock is already held.
To release the Mutex, use Mutex::unlock()
LOCK-ERROR | lock called twice in the same thread, object deleted in another thread, etc |
THREAD-DEADLOCK | a deadlock was detected while trying to acquire the lock |
int Qore::Thread::Mutex::lock | ( | timeout | timeout_ms | ) |
Locks the Mutex object; blocks if the lock is already held.
An optional timeout value may be passed to this method, giving a time in milliseconds to wait for the lock to become free. Like all Qore functions and methods taking timeout values, a relative time value may be passed instead of an integer to make the timeout units clear
To release the Mutex, use Mutex::unlock()
timeout_ms | a timeout value to wait to acquire the lock; integers are interpreted as milliseconds; relative date/time values are interpreted literally (with a resolution of milliseconds) |
LOCK-ERROR | lock called twice in the same thread, object deleted in another thread, etc |
THREAD-DEADLOCK | a deadlock was detected while trying to acquire the lock |
int Qore::Thread::Mutex::trylock | ( | ) |
Acquires the lock only if it is not already held; returns 0 for success (lock acquired) or -1 if the call would block.
LOCK-ERROR | object deleted in another thread, etc |
THREAD-DEADLOCK | a deadlock was detected while trying to acquire the lock |
nothing Qore::Thread::Mutex::unlock | ( | ) |
Unlocks the Mutex object; wakes up one thread if any threads are blocked on this lock.
LOCK-ERROR | unlock called by a thread that does not own the lock or the lock is not locked, object deleted in another thread, etc |