Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <boost/thread.hpp>
#include <iostream>
int main()
{
boost::shared_mutex mutex;
std::cerr <<"many shared locks!\n";
boost::shared_lock<boost::shared_mutex> slock1(mutex);
boost::shared_lock<boost::shared_mutex> slock2(mutex);
boost::shared_lock<boost::shared_mutex> slock3(mutex);
boost::shared_lock<boost::shared_mutex> slock4(mutex);
boost::shared_lock<boost::shared_mutex> slock5(mutex);
std::cerr <<"get upgrade_lock lock\n";
boost::upgrade_lock<boost::shared_mutex> wlock(mutex); // write lock
// boost::upgrade_lock<boost::shared_mutex> write_lock(mutex); // upgrade_lock同士は共存できないので確保不能
// boost::upgrade_to_unique_lock<boost::shared_mutex> write_lock(wlock); // shared_lockが全部開放されるまで確保不能
boost::shared_lock<boost::shared_mutex> slock6(mutex); // shared_lockは追加で獲得可能
std::cerr <<"end\n"; // shared_lockとupgrade_lockは共存できるのでこのプログラムはendと出力して終了する
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.