Overview of Boost Lock:
The Boost library provides a mutex class and a lock class that can be easily constructed by combining read-write and mutex locks.
For a popular example, if the shared resource is an automatically locked room, the mutex is the key, the key must be taken into the room, and the key should be left in the room. This corresponds to the lock(take key) and unlock(the key) of the mutex.
a similar situation exists for dynamically allocating memory. If you forgetDelete, which can cause a memory leak. How is it solved? assigning an object on a stack is a feature that, after leaving the scope, the object is sure to call the Destructor method. With this feature, you can use object-to-pointer encapsulation, in the object's destructor methodDelete, it guarantees that it will be executed.Delete. This is the smart pointer. Because the smart pointer is not for a specific type of pointer, the smart pointer is designed as a template for the class, which is an exception to a templated class based on the stencil argument. Similarly, the same technique can be used to encapsulate the mutex. This isLockclass template. In theLockof the constructor method that invokes the mutex.Lockmethod, inLockthe destructor of the method that invokes the mutex.Unlockmethod. Mutexis the object class,Lockis a template class.
Mutex object class:
There are two main types of mutex classes: Boost::mutex,boost::shared_mutex, in which mutexes have lock and unlock methods, Shared_mutex In addition to the lock and unlock methods, there are shared_ Lock and Shared_unlock methods. Therefore, Boost::mutex is an exclusive mutex class, and Boost::shared_mutex is a shared mutex class.
Lock template class:
Boost::unique_lock<t>,boost::shared_lock<t>, where Unique_lock is an exclusive lock, Shared_lock is a shared lock. The T in unique_lock<t> can be any of the mutex classes, and if it is Shared_mutex, then boost::unique_lock<boost::shared_mutex> When the object constructor of a class is constructed, the Shared_lock method of Shared_mutex is automatically called, and the Shared_unlock method of Shared_mutex is automatically called in the destructor. If it is boost:: Unique_lock<boost::mutex>, the Lock and unlock methods are automatically called respectively.
T in boost::shared_lock<t> can only be the Shared_mutex class.
Implementation of read-write locks:
Typedefboost::shared_lock<boost::shared_mutex> Readlock;
typedef boost::unique_lock<boost::shared_mutex> Writelock;
Boost::shared_mutex Rwmutex;
void ReadOnly ()
{
Readlock Rdlock (Rwmutex);
Do something
}
void WriteOnly ()
{
Writelock Wtlock (Rwmutex);
Do something
}
For the same Rwmutex, threads can have multiple readlock at the same time, and these readlock block any thread that attempts to acquire Writelock until all Readlock objects are refactored. If Writelock first obtains the Rwmutex, it blocks any thread that attempts to gain Readlock or Writelock on Rwmutex. Boost::shared_lock use be careful, never enter the same thread multiple times.
The implementation of the mutex:
typedef boost::unique_lock<boost::mutex> Exclusivelock;
Recursive Mutex amount:
The Boost::recursive_mutex provides a recursive mutex amount. For an instance to allow at most one thread to have its lock, if a thread has locked a Boost::recursive_mutex instance, the thread can lock the instance multiple times.
Boost::mutex::scoped_lock
Boost::mutexio_mutex;
void Foo ()
{
{
Boost::mutex::scoped_lock Lock (Io_mutex); Lock
} // unlock
}
Boost Lock Usage Summary