FX::QShrdMemMutex Class Reference

#include <QThread.h>

List of all members.


Detailed Description

A very lightweight mutex object to synchronise access to shared memory regions.

What goes for FX::QMutex goes for this. However, there are further FXRESTRICTions: because inter-process mutex support is not available on all platforms, this object provides a working alternative based on FX::FXAtomicInt. It however does not invoke kernel waits so hence your process will waste processor time spinning on the lock if it is held by some other process. Hence it is very important that your shared memory region is very very rarely in contention.

One other problem is what happens if the process dies suddenly while holding the lock. In this situation the lock must be unlocked at best, but at worst the shared memory state may be damaged and thus all other applications will be adversely affected. This object times out and gives you the lock anyway after a second by default, but you must take care in your code to hold this lock for only the very shortest period of time possible and do nothing which could cause you to fail to unlock it. Look into FX::FXDoUndo.

Because of all these problems, it is advisable that for anything more than trivial use of shared memory, another synchronisation method should be used eg; IPC messaging.

As of v0.86, defining FXINLINE_MUTEX_IMPLEMENTATION defines this class inline to all code including QThread.h. This can cause substantial performance improvement at the cost of code size.

Public Member Functions

QMUTEX_INLINEP QShrdMemMutex (FXuint _timeout=1000)
QMUTEX_INLINEP FXuint timeOut () const throw ()
QMUTEX_INLINEP void setTimeOut (FXuint to) throw ()
QMUTEX_INLINEP void lock ()
QMUTEX_INLINEP void unlock () throw ()
QMUTEX_INLINEP bool tryLock ()


Constructor & Destructor Documentation

QMUTEX_INLINEP FX::QShrdMemMutex::QShrdMemMutex ( FXuint  _timeout = 1000  )  [inline]

Constructs an instance. Using FXINFINITE means infinite waits.


Member Function Documentation

QMUTEX_INLINEP FXuint FX::QShrdMemMutex::timeOut (  )  const throw () [inline]

Returns the time after which a lock succeeds anyway.

QMUTEX_INLINEP void FX::QShrdMemMutex::setTimeOut ( FXuint  to  )  throw () [inline]

Sets the time after which a lock succeeds anyway.

QMUTEX_INLINEP void FX::QShrdMemMutex::lock (  ) 

Locks the shared memory mutex.

References FX::FXProcess::getMsCount(), FX::FXAtomicInt::swapI(), and FX::QThread::yield().

QMUTEX_INLINEP void FX::QShrdMemMutex::unlock (  )  throw () [inline]

Unlocks the shared memory mutex.

QMUTEX_INLINEP bool FX::QShrdMemMutex::tryLock (  ) 

Tries the shared memory mutex.

References FX::FXAtomicInt::swapI().


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

(C) 2002-2008 Niall Douglas. Some parts (C) to assorted authors.
Generated on Fri Jun 13 22:28:52 2008 for TnFOX by doxygen v1.5.6