If you can read this text, it means you are not experiencing this website at its best. This website is designed for used with a standards-compliant browser.
Current version: 2.3.2
ZThreads
A platform-independent, multi-threading and synchronization library for C++
Home Documentation Downloads CVS Contact
Mutex Class Reference

#include <Mutex.h>

Inheritance diagram for Mutex:
Lockable NonCopyable

Public Member Functions

 Mutex ()
 Create a new Mutex.
 
virtual ~Mutex ()
 Destroy this Mutex.
 
virtual void acquire ()
 
virtual bool tryAcquire (unsigned long timeout)
 
virtual void release ()
 
- Public Member Functions inherited from Lockable
virtual ~Lockable ()
 Destroy a Lockable object.
 
virtual void acquire ()=0
 
virtual bool tryAcquire (unsigned long timeout)=0
 
virtual void release ()=0
 

Detailed Description

Author
Eric Crahen http://www.code-foo.com
Date
<2003-07-16T19:35:28-0400>
Version
2.2.1

A Mutex is used to provide serialized (one thread at a time) access to some portion of code. This is accomplished by attempting to acquire the Mutex before entering that piece of code, and by releasing the Mutex when leaving that region. It is a non-reentrant, MUTual EXclusion Lockable object.

See also
Guard

Scheduling

Threads competing to acquire() a Mutex are granted access in FIFO order.

Error Checking

A Mutex will throw a Deadlock_Exception if an attempt to acquire a Mutex more than once is made from the context of the same thread.

A Mutex will throw an InvalidOp_Exception if an attempt to release a Mutex is made from the context of a thread that does not currently own that Mutex.

Member Function Documentation

◆ acquire()

virtual void acquire ( )
virtual

Acquire a Mutex, possibly blocking until either the current owner of the Mutex releases it or until an exception is thrown.

Only one thread may acquire() the Mutex at any given time.

Exceptions
Interrupted_Exceptionthrown when the calling thread is interrupted. A thread may be interrupted at any time, prematurely ending any wait.
Deadlock_Exceptionthrown when the same thread attempts to acquire a Mutex more than once, without having first release()ed it.
Precondition
the calling thread must not have already acquired Mutex
Postcondition
the calling thread successfully acquired Mutex only if no exception was thrown.
See also
Lockable::acquire()

Implements Lockable.

◆ release()

virtual void release ( )
virtual

Release a Mutex allowing another thread to acquire it.

Exceptions
InvalidOp_Exception- thrown if there is an attempt to release is a Mutex that was not owner by the calling thread.
Precondition
the calling thread must have first acquired the Mutex.
Postcondition
the calling thread successfully released Mutex only if no exception was thrown.
See also
Lockable::release()

Implements Lockable.

◆ tryAcquire()

virtual bool tryAcquire ( unsigned long  timeout)
virtual

Acquire a Mutex, possibly blocking until the current owner of the Mutex releases it, until an exception is thrown or until the given amount of time expires.

Only one thread may acquire the Mutex at any given time.

Parameters
timeoutmaximum amount of time (milliseconds) this method could block
Returns
  • true if the lock was acquired
  • false if the lock was acquired
Exceptions
Interrupted_Exceptionthrown when the calling thread is interrupted. A thread may be interrupted at any time, prematurely ending any wait.
Deadlock_Exceptionthrown when the same thread attempts to acquire a Mutex more than once, without having first released it.
Precondition
the calling thread must not have already acquired Mutex
Postcondition
the calling thread successfully acquired Mutex only if no exception was thrown.
See also
Lockable::tryAcquire(unsigned long timeout)

Implements Lockable.


The documentation for this class was generated from the following file: