[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]

 

Chapter 22.

 

Mutex.h



This chapter is a reference guide to the mutex support library.


Overview of Mutex Support Library

The mutex "mutual exclusion" support library provides the mechanism for implementing thread-safe applications.

It is important to remember that <mutex.h> does not attempt to be a complete thread library. It merely implements just enough for the C++ library's implementation. Programs with needs greater than this should use the facilities of the native operating system.

The mutex support library consists of the following classes:


Header <Mutex.h>

The header mutex.h is used for mutual exclusion to provide thread-safe applications.

Class Mutex Synopsis:


namespace Metrowerks{
class mutex
{
	public:
		mutex();
		~mutex();
		void lock();
		void unlock();
	private:
		mutex(const mutex&);             // Not defined
		mutex& operator=(const mutex&);  // Not defined
};

Class mutex_lock Synopsis:
class mutex_lock
	{
	public:
		explicit mutex_lock(mutex& m) : m_(m) {m_.lock();}
		~mutex_lock() {m_.unlock();}
	private:
		mutex& m_;
		mutex_lock(const mutex_lock&);
		mutex_lock& operator=(const mutex_lock&);

}; // end namespace Metrowerks


Mutex

The mutex class provides for the basic mutual exclusion mechanism.

synopsis:

class mutex {


public:
  mutex();
  ~mutex();
  void lock();
  void unlock();
private:
  mutex(const mutex&);
  mutex& operator=(const mutex&);
};

For example of using the mutex class see:"Example of mutex_lock."


Constructors and Assignment Operator

Initialize the mutex object.

Prototype:

mutex ();
  private:
mutex(const mutex&);

A default and a copy constructor are defined.

The copy constructor is declared private and not defined to prevent the mutex object from being copied.


Operator=

The assignment operator for the Mutex Class.

Prototype:

private:
  mutex& operator=(const mutex&);

The assignment operator is declared private and not defined to prevent the mutex object from being copied.


Destructor

Used for implicit mutex destruction.

Prototype:

~mutex ();

Destroys the mutex object.


Public Member Functions

Public members that provide for mutual exclusion.


Lock

Locks the object into exclusion.

Prototype:

void lock();

If locked the object is prevented from being used.


For example of using mutex::lock() see:"Example of mutex_lock."


Unlock

Unlocks the exclusion.

Prototype:

void unlock();
:

If unlocked the object is allowed to be reused.


Mutex_lock

The class mutex_lock is used so that a mutex object won't accidently be left locked after an exception is thrown.

Prototype:

class mutex_lock {
  public:
  explicit mutex_lock(mutex& m);
  ~mutex_lock() ;
  private:
  mutex& m_;
  mutex_lock(const mutex_lock&);
  mutex_lock& operator=(const mutex_lock&);
};

Constructors and Assignment Operator

Initialize the mutex_lock object.

Prototype :

inline explicit 
  mutex_lock(mutex& m) : m_(m) m_.lock();}
private:
  mutex_lock(const mutex_lock&);

The copy constructor is declared private and not defined to prevent the mutex_lock object from being copied.


Operator=

The assignment operator for mutex_lock.

Prototype :

mutex_lock& operator=(const mutex_lock&);

The assignment operator is declared private and not defined to prevent the mutex_lock object from being copied.


Destructor

Destroys the mutex_lock object.

Prototype :

mutex_lock() {m_.unlock();}

Destroys the mutex_lock object.

Example of mutex_lock:


class A
{
public:
	void foo();
private:
	mutex mut_;
};
void A::foo()
{
	mut_.lock();
	// do some stuff
	mut_.unlock();  // not exception safe!
}





If "do some stuff" should throw an exception then mut_ will remain locked with no good way to unlock it.  So you should instead use:





void A::foo()
{
     mutex_lock lock(mut_);
     // do some stuff
}

 


 

 


[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]
Visit the Metrowerks website at: http://www.metrowerks.com
For assistance contact Metrowerks Technical Support at: cw_support@metrowerks.com
Copyright © 2000, Metrowerks Corp. All rights reserved.

Last updated: July 21, 2000