码迷,mamicode.com
首页 > 编程语言 > 详细

multithreading - Reader/Writer Locks in C++

时间:2015-01-20 20:01:12      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

 

You Only Need To Note This: only 1 single thread can acquire an upgrade_lock at one time.

 

 

others are very straightforward.

96 vote

1800 INFORMATION is more or less correct, but there are a few issues I wanted to correct.

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
}

 

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 into it). So, if all your readers are conditional writers, you need to find another solution.

 
 
 
  

 

Test code: 

 

  1 /*‘‘‘
  2 
  3 [[[[[[[[[[[[[[[                                 ]]]]]]]]]]]]]]]
  4 [::::::::::::::                                 ::::::::::::::]
  5 [::::::::::::::                                 ::::::::::::::]
  6 [::::::[[[[[[[:                                 :]]]]]]]::::::]
  7 [:::::[                                                 ]:::::]
  8 [:::::[                                                 ]:::::]
  9 [:::::[                                                 ]:::::]
 10 [:::::[                                                 ]:::::]
 11 [:::::[   Created on Dec 25, 2014 (merry christmas)     ]:::::]
 12 [:::::[   @author: ScottGu<150316990@qq.com,            ]:::::]
 13 [:::::[                     gu.kai.66@gmail.com>        ]:::::]
 14 [:::::[   performance tested:                           ]:::::]
 15 [:::::[   environment: 64bit win7, i7-4800MQ, 8GB       ]:::::]
 16 [:::::[                                                 ]:::::]
 17 [:::::[                                                 ]:::::]
 18 [:::::[                                                 ]:::::]
 19 [:::::[                                                 ]:::::]
 20 [::::::[[[[[[[:                                 :]]]]]]]::::::]
 21 [::::::::::::::                                 ::::::::::::::]
 22 [::::::::::::::                                 ::::::::::::::]
 23 [[[[[[[[[[[[[[[                                 ]]]]]]]]]]]]]]]]
 24 
 25 ‘‘‘*/
 26 
 27 #pragma once
 28 
 29 #include "stdafx.h"
 30 #include <cstdlib>
 31 #include <iostream>
 32 #include <string>
 33 
 34 #include "boost/bind.hpp"
 35 #include "boost/smart_ptr.hpp"
 36 #include "boost/asio.hpp"
 37 #include "boost/thread/thread.hpp"
 38 #include "boost/date_time/posix_time/ptime.hpp"
 39 #include <boost/format.hpp>
 40 
 41 #include <boost/thread/mutex.hpp>
 42 #include <boost/thread/locks.hpp>
 43 #include <boost/algorithm/string/predicate.hpp>
 44 #include <boost/exception/diagnostic_information.hpp> 
 45 #include <boost/exception_ptr.hpp> 
 46 #include <boost/thread/shared_mutex.hpp>
 47 #include <boost/lexical_cast.hpp>
 48 
 49 
 50 typedef boost::shared_mutex Lock;
 51 typedef boost::unique_lock< Lock > WriteLock_uniq;
 52 typedef boost::upgrade_lock< Lock > WriteLock_upgrade;
 53 typedef boost::shared_lock< Lock > ReadLock;
 54 
 55 Lock myLock;
 56 
 57 
 58 
 59 void writer_thread() {
 60     WriteLock_uniq w_lock(myLock);
 61     std::string threadId = boost::lexical_cast<std::string>(boost::this_thread::get_id());
 62     std::cout << "writer holding lock, threadid " << threadId << " " << std::endl;
 63     Sleep(1000);
 64     std::cout << "END, threadid " << threadId << " " << std::endl;
 65 
 66 };
 67 
 68 void writer_upgrade_thread() {
 69     WriteLock_upgrade w_lock(myLock);
 70     std::string threadId = boost::lexical_cast<std::string>(boost::this_thread::get_id());
 71     std::cout << "UPgraded writer holding lock, threadid " << threadId << " " << std::endl;
 72     Sleep(1000);
 73     std::cout << "END, threadid " << threadId << " " << std::endl;
 74 
 75 };
 76 
 77 
 78 void reader_thread() {
 79     ReadLock r_lock(myLock);
 80     std::string threadId = boost::lexical_cast<std::string>(boost::this_thread::get_id());
 81     std::cout << "reader holding lock, threadid " << threadId << " " << std::endl;
 82     Sleep(1000);
 83     std::cout << "END, threadid " << threadId << " " << std::endl;
 84 
 85 };
 86 
 87 
 88 int test_RW_lock() 
 89 {
 90     boost::thread_group threads;
 91 
 92     threads.create_thread(boost::bind(reader_thread));
 93     threads.create_thread(writer_upgrade_thread);
 94     threads.create_thread(writer_upgrade_thread);
 95     threads.create_thread(boost::bind(reader_thread));
 96     threads.create_thread(boost::bind(reader_thread));
 97     threads.create_thread(boost::bind(reader_thread));
 98     threads.create_thread(boost::bind(reader_thread));
 99 
100     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();
105 }

 

multithreading - Reader/Writer Locks in C++

标签:

原文地址:http://www.cnblogs.com/scottgu/p/4236549.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!