omnithread library

Brian Burton brian.burton@burton-computer.com
Tue, 27 May 1997 09:31:46 -0400


I'm porting the omnithread library to BeOS (http://www.be.com) and
noticied that there are no classes in the omnithread.h file for
acquiring and releasing mutexes and semaphores.  These really should be
added since they would be far more convenient and less error prone than
calling lock()/unlock() directly.  For example:

class omni_mutex_lock
{
public:
    omni_mutex_lock(omni_mutex *mutex)
      : _mutex(mutex)
    {
        _mutex->lock();
    }
    ~omni_mutex_lock()
    {
        _mutex->unlock();
    }
private:
    omni_mutex *_mutex;
};


A similar class could be added for semaphores (possibly with an
overloaded constructor for doing a try_wait).

With a class such as this, a mutex lock can be acquired and released by
simply defining a variable of the omni_mutex_lock class:


  omni_mutex mutex;

   ...

  int some_class::some_function()
  {
      omni_mutex_lock lock(&mutex);
      if (some_flag)
         return -1;
      do_something_that_might_throw_exception();
      return 0;
  }

The lock object's destructor would automatically unlock the mutex when
the return statements are executed and when an exception is encountered.

Are there already helper classes like this specified in another header? 
If not, then they should probably be added to omnithread.h to provide a
standard version.

All the best,
++Brian

-- 
Brian Burton                          Custom Software Development
Burton Computer Corporation           Java, C++, Orbix, Tuxedo
brian.burton@burton-computer.com      OO Consulting and Mentoring