Appendices > Portable Thread Library > Mutexes > Using Mutex Lockers
 
Using Mutex Lockers
A mutex is often used to lock access to a resource in a member function and to unlock the resource when the function returns. To facilitate this behavior, the portable thread library provides two classes IlsUnsafeMutexLocker and IlsSafeMutexLocker. These classes are mutex lockers which are just mutex wrappers that lock their associated mutex in the constructor and unlock it in the destructor. These classes also provide an assign member function which can be used to assign a mutex to the locker or deassign it in order to override the member functions lock and unlock in the constructor and destructor.
Here is an example:
class myConnection
{
public:
   ...
   void deleteConnection();
private:
   IlsUnsafeMutex _protectConnection;
   conn* _connection;
};
 
void
myConnection::deleteConnection()
{
IlsUnsafeMutexLocker locker(&_protectConnection);
delete _connection;
_connection = 0;
};
In that example, the mutex locker locks the mutex which is used to protect the private data member _connection and unlocks the mutex when the member function deleteConnection returns.

Version 6.1
Copyright © 2016, Rogue Wave Software, Inc. All Rights Reserved.