Skip to content

Instantly share code, notes, and snippets.

@stevenRush
Created March 6, 2014 07:50
Show Gist options
  • Save stevenRush/9384415 to your computer and use it in GitHub Desktop.
Save stevenRush/9384415 to your computer and use it in GitHub Desktop.
Shared mutex upgrade example
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
}
void unconditional_writer()
{
boost::unique_lock< boost::shared_mutex > lock(_access);
// do work here, with exclusive access
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment