I am very confused about the difference between a lock and mutex. In Boost docs, it says,
Lock Types
- Class template lock_guard
- Class template unique_lock
- Class template shared_lock
- Class template upgrade_lock
- Class template upgrade_to_unique_lock
- Mutex-specific class scoped_try_lock
Mutex Types
- Class mutex
- Typedef try_mutex
- Class timed_mutex
- Class recursive_mutex
- Typedef recursive_try_mutex
- Class recursive_timed_mutex
- Class shared_mutex
In another article, I see functions like this,
boost::shared_mutex _access;
void reader()
{
boost::shared_lock< boost::shared_mutex > lock(_access);
// do work here, without anyone having exclusive access
}
void conditional_writer()
{
boost::upgrade_lock< boost::shared_mutex > lock(_access);
// do work here, without anyone having exclusive access
if (something) {
boost::upgrade_to_unique_lock< boost::shared_mutex > uniqueLock(lock);
// do work here, but now you have exclusive access
}
// do more work here, without anyone having exclusive access
}
Updated questions
- Can anyone offer some clarification between the "mutex" and "lock"?
- Is it necessary to create a shared_lock for a shared_mutex? What happen if I create a unique_lock for a shared_mutex?
- Or if I create a shared_lock for a mutex, does it mean the mutex can not be shared among multiple threads?