last update 20 Sep 2009 |
#include <cvrMutex.h>
Public Member Functions | |
mutex () | |
virtual | ~mutex () |
void | lock () |
bool | tryLock () |
void | unlock () |
Protected Member Functions | |
void | destroy () |
Protected Attributes | |
pthread_mutex_t | theMutex_ |
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.
cvr::mutex::mutex | ( | ) |
Default constructor with unlocked mutex.
virtual cvr::mutex::~mutex | ( | ) | [virtual] |
Destructor.
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 | ( | ) |
void cvr::mutex::unlock | ( | ) |
pthread_mutex_t cvr::mutex::theMutex_ [protected] |