Multithreading-reader/writer Locks in C + +

Source: Internet
Author: User

You have need to Note this:only 1 single thread can acquire a upgrade_lock at one time.

Others is very straightforward.
Vote

1800 information was more or less correct, but there was a few issues I wanted to correct.

Boost::shared_mutex _access;voidReader () {Boost::shared_lock< Boost::shared_mutex >Lock(_access); //Do work here , without anyone have exclusive access}voidConditional_writer () {Boost::upgrade_lock< Boost::shared_mutex >Lock(_access); //Do work here , without anyone have exclusive access  if(something) {Boost::upgrade_to_unique_lock< Boost::shared_mutex > Uniquelock (Lock); //Do exclusive access  }  //Do more work here , without anyone have exclusive access}voidUnconditional_writer () {Boost::unique_lock< Boost::shared_mutex >Lock(_access); //Do work here , with exclusive access}

Also Note, unlike a shared_lock, only a single thread can acquire an upgrade_lock at one time, even when it isn ' t upgraded (which I thought was awkward when I ran to it). So, if all your readers is conditional writers, you need to find another solution.

Test Code:

1 /*" "2 3 [[[[[[[[[[[[[[[                                 ]]]]]]]]]]]]]]]4 [::::::::::::::                                 ::::::::::::::]5 [::::::::::::::                                 ::::::::::::::]6 [::::::[[[[[[[:                                 :]]]]]]]::::::]7 [:::::[                                                 ]:::::]8 [:::::[                                                 ]:::::]9 [:::::[                                                 ]:::::]Ten [:::::[                                                 ]:::::] One [-::::: [Created on Dec, Merry Christmas]: ::::] A [::::: [@author: Scottgu<[email protected],]: ::::] - [::::: [[email protected]>]: ::::] - [::::: [Performance Tested:]:: :::] the [::::: [Environment:64bit win7, I7-4800MQ, 8GB]: ::::] - [:::::[                                                 ]:::::] - [:::::[                                                 ]:::::] - [:::::[                                                 ]:::::] + [:::::[                                                 ]:::::] - [::::::[[[[[[[:                                 :]]]]]]]::::::] + [::::::::::::::                                 ::::::::::::::] A [::::::::::::::                                 ::::::::::::::] at [[[[[[[[[[[[[[[                                 ]]]]]]]]]]]]]]]] -  - " "*/ -  - #pragmaOnce -  in#include"stdafx.h" -#include <cstdlib> to#include <iostream> +#include <string> -  the#include"boost/bind.hpp" *#include"boost/smart_ptr.hpp" $#include"boost/asio.hpp"Panax Notoginseng#include"boost/thread/thread.hpp" -#include"boost/date_time/posix_time/ptime.hpp" the#include <boost/format.hpp> +  A#include <boost/thread/mutex.hpp> the#include <boost/thread/locks.hpp> +#include <boost/algorithm/string/predicate.hpp> -#include <boost/exception/diagnostic_information.hpp> $#include <boost/exception_ptr.hpp> $#include <boost/thread/shared_mutex.hpp> -#include <boost/lexical_cast.hpp> -  the  - typedef boost::shared_mutex Lock;Wuyitypedef boost::unique_lock< LOCK >Writelock_uniq; thetypedef boost::upgrade_lock< LOCK >Writelock_upgrade; -typedef boost::shared_lock< LOCK >Readlock; Wu  - Lock MyLock; About  $  -  - voidWriter_thread () { - Writelock_uniq W_lock (myLock); ASTD::stringThreadId = boost::lexical_cast<std::string>(boost::this_thread::get_id ()); +Std::cout <<"writer Holding lock, ThreadID"<< threadId <<" "<<Std::endl; theSleep ( +); -Std::cout <<"END, ThreadID"<< threadId <<" "<<Std::endl; $  the }; the  the voidWriter_upgrade_thread () { the writelock_upgrade W_lock (myLock); -STD::stringThreadId = boost::lexical_cast<std::string>(boost::this_thread::get_id ()); inStd::cout <<"upgraded writer holding lock, ThreadID"<< threadId <<" "<<Std::endl; theSleep ( +); theStd::cout <<"END, ThreadID"<< threadId <<" "<<Std::endl; About  the }; the  the  + voidReader_thread () { - readlock R_lock (myLock); theSTD::stringThreadId = boost::lexical_cast<std::string>(boost::this_thread::get_id ());BayiStd::cout <<"Reader holding lock, ThreadID"<< threadId <<" "<<Std::endl; theSleep ( +); theStd::cout <<"END, ThreadID"<< threadId <<" "<<Std::endl; -  - }; the  the  the intTest_rw_lock () the { - Boost::thread_group Threads; the  the Threads.create_thread (Boost::bind (Reader_thread)); the Threads.create_thread (writer_upgrade_thread);94 Threads.create_thread (writer_upgrade_thread); the Threads.create_thread (Boost::bind (Reader_thread)); the Threads.create_thread (Boost::bind (Reader_thread)); the Threads.create_thread (Boost::bind (Reader_thread));98 Threads.create_thread (Boost::bind (Reader_thread)); About  - Threads.create_thread (writer_upgrade_thread);101 Threads.create_thread (writer_upgrade_thread);102 Threads.create_thread (Boost::bind (Writer_thread));103 104 Threads.join_all (); the}

Multithreading-reader/writer Locks in C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.