lock keyword in C++
lock (m_mutex)
{
// my protected code here
} |
lock keyword, but you can make one yourself. Given a
Mutex class which has Lock() and Unlock() member functions
(and perhaps an IsLocked() for convenience) most C++ programmers would immediately
write an AutoLock, somewhat like this:
class AutoLock
{
public:
AutoLock(Mutex& m): m_mutex(m) { m_mutex.Lock(); }
~AutoLock() { m_mutex.Unlock(); }
operator bool() { return m_mutex.IsLocked(); }
private:
Mutex& m_mutex;
}; |
{
AutoLock lock(m_mutex);
// my protected code here
} |
#define lock(x) if (!(AutoLock _l = x)); else |
Sander Stoks – Last edit: 31 Jan 2007