CVR-Lib last update 20 Sep 2009

cvr::mutex Class Reference

Mutex class for the library. More...

#include <cvrMutex.h>

List of all members.

Public Member Functions

 mutex ()
virtual ~mutex ()
void lock ()
bool tryLock ()
void unlock ()

Protected Member Functions

void destroy ()

Protected Attributes

pthread_mutex_t theMutex_


Detailed Description

Mutex class for the library.

This object can be used to protect critical sections on multithreaded applications. The same thread should NOT try to lock the mutex more than once. The behavior of this will depend on the operating system: on linux/unix the thread will be locked forever (posix standard); on windows, the thread will count how many lock have been done, but it will not be blocked by the later locks!

Example:

 // A class with some code to be protected
 class A {
 private:
   // The mutex used to protect the some code blocks
   cvr::mutex lock_;

   // Data that requires exclusive access, e.g. a std::list
   std::list<int> data_;

 public:
   // A method that requires some exclusive access
   void access() {
     lock_.lock();   // ensure exclusive access to the data
     static int numAccesses = 0;
     data_.push_back(numAccesses++);
     lock_.unlock(); // realease exclusive access
   }
 };

In the previous example the "access()" method ensures that only one thread at a time has access to the data_ attribute of the class. Other methods that also access the data_ attribute should also protect the access.

See also:
cvr::semaphore, cvr::thread

Constructor & Destructor Documentation

cvr::mutex::mutex (  ) 

Default constructor with unlocked mutex.

virtual cvr::mutex::~mutex (  )  [virtual]

Destructor.


Member Function Documentation

void cvr::mutex::destroy (  )  [protected]

Destroy the mutex.

void cvr::mutex::lock (  ) 

Wait until lock for mutex becomes available and lock it.

Referenced by cvr::lapackInterface::lockInterface().

bool cvr::mutex::tryLock (  ) 

Try to lock mutex, but do not block.

Returns:
true if locking was successful.

void cvr::mutex::unlock (  ) 


Member Data Documentation

pthread_mutex_t cvr::mutex::theMutex_ [protected]

the posix mutex object


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

Generated on Sun Sep 20 22:09:06 2009 for CVR-Lib by Doxygen 1.5.8