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.

 
 
  
#pragma once

#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <string> #include "boost/bind.hpp"
#include "boost/smart_ptr.hpp"
#include "boost/asio.hpp"
#include "boost/thread/thread.hpp"
#include "boost/date_time/posix_time/ptime.hpp"
#include <boost/format.hpp> #include <boost/thread/mutex.hpp>
#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> typedef boost::shared_mutex Lock;
typedef boost::unique_lock< Lock > WriteLock_uniq;
typedef boost::upgrade_lock< Lock > WriteLock_upgrade;
typedef boost::shared_lock< Lock > ReadLock; Lock myLock; void writer_thread() {
WriteLock_uniq w_lock(myLock);
std::string threadId = boost::lexical_cast<std::string>(boost::this_thread::get_id());
std::cout << "writer holding lock, threadid " << threadId << " " << std::endl;
Sleep();
std::cout << "END, threadid " << threadId << " " << std::endl; }; void writer_upgrade_thread() {
WriteLock_upgrade w_lock(myLock);
std::string threadId = boost::lexical_cast<std::string>(boost::this_thread::get_id());
std::cout << "UPgraded writer holding lock, threadid " << threadId << " " << std::endl;
Sleep();
std::cout << "END, threadid " << threadId << " " << std::endl; }; void reader_thread() {
ReadLock r_lock(myLock);
std::string threadId = boost::lexical_cast<std::string>(boost::this_thread::get_id());
std::cout << "reader holding lock, threadid " << threadId << " " << std::endl;
Sleep();
std::cout << "END, threadid " << threadId << " " << std::endl; }; int test_RW_lock()
{
boost::thread_group threads; threads.create_thread(boost::bind(reader_thread));
threads.create_thread(writer_upgrade_thread);
threads.create_thread(writer_upgrade_thread);
threads.create_thread(boost::bind(reader_thread));
threads.create_thread(boost::bind(reader_thread));
threads.create_thread(boost::bind(reader_thread));
threads.create_thread(boost::bind(reader_thread)); threads.create_thread(writer_upgrade_thread);
threads.create_thread(writer_upgrade_thread);
threads.create_thread(boost::bind(writer_thread)); threads.join_all();
}

multithreading - Reader/Writer Locks in C++的更多相关文章

  1. RFID 读写器 Reader Writer Cloner

    RFID读写器的工作原理 RFID的数据采集以读写器为主导,RFID读写器是一种通过无线通信,实现对标签识别和内存数据的读出和写入操作的装置. 读写器又称为阅读器或读头(Reader).查询器(Int ...

  2. Stream,Reader/Writer,Buffered的区别(1)

    Stream: 是字节流形式,exe文件,图片,视频等.支持8位的字符,用于 ASCII 字符和二进制数据. Reader/Writer: 是字符流,文本文件,XML,txt等,用于16位字符,也就是 ...

  3. RFIDler - An open source Software Defined RFID Reader/Writer/Emulator

    https://www.kickstarter.com/projects/1708444109/rfidler-a-software-defined-rfid-reader-writer-emul h ...

  4. RubyMine生成reader/writer方法

    RubyMine生成reader/writer方法 在非类的ruby文件中,Alt+Insert会出现新建文件的选项: 在ruby文件的类中,Alt+Insert会出现get/set方法生成提示和重构 ...

  5. Stream,Reader/Writer,Buffered的区别(2)

    Reader: Reader的子类: 1.BufferedReader: FileReader 没有提供读取文本行的功能,BufferedReader能够指定缓冲区大小,包装了read方法高效读取字符 ...

  6. ExtJS4.2学习(7)——基础知识之Reader&Writer篇

    Reader: 主要用于将proxy数据代理读取的数据按照不同的规则进行解析,将解析好的数据保存到Modle中. 结构图 Ext.data.reader.Reader 读取器的根类(很少直接实例化这个 ...

  7. 02_IO操作的基本规律(InputStream,OutputStream,Reader,Writer,FileReader,FileWriter,BufferedReader,BufferedWri

     模拟BufferedInputStream,编写一个类 package toto.IO; import java.io.IOException; import java.io.InputStre ...

  8. Python CSV Reader/Writer 例子--转载

    CSV(comma-separated values) 是跨多种形式导入导出数据的标准格式,比如 MySQL.Excel. 它以纯文本存储数和文本.文件的每一行就代表一条数据,每条记录包含了由逗号分隔 ...

  9. Java I/O流-总结(InputStream,OutputStream,Reader,Writer)

    Java流总结 一. 流的分类 • 按数据流动方向 – 输入流:只能从中读取字节数据,而不能向其写出数据 – 输出流:只能向其写入字节数据,而不能从中读取数据 • 按照流所处理的数据类型 – 字节流: ...

随机推荐

  1. RHEL6.4编译安装企业级LAMMP平台

    一.LAMMP简介 二.使用软件及服务器架构说明 三.配置及安装过程    1.安装arp与httpd    2.安装mysql    3.安装php(php-fpm)    4.安装Xcache   ...

  2. LCD底层驱动分析

    根据分析的框架,自己写一个LCD驱动程序 1分析LCD硬件原理图 Von和Voff接的是一个电源电路,通过LCD_POWER接的是GPG4来控制LCD电源,高电平表示开启LCD电源 VM接的是CPU的 ...

  3. 使用javaScript实现简单倒计时功能

    效果如下: <div class="warp"> <p id="txt">距离”十一“国庆放假还有:</p><br&g ...

  4. 2014年7月份第1周51Aspx源码发布详情

      QF万能视频播放器源码  2014-6-30 [VS2010]本源码是一个万能视频播放器源码.可实现各种格式的影片播放功能. 1.点击[开始]按钮,弹出窗口,选择影片路径,确定后即可播放.可拖拽滚 ...

  5. 火狐浏览器不能获取 event,解决js报错问题

    //←的keyCode等于:37 //→的keyCode等于:39 //↑的keyCode等于:38 //↓的keyCode等于:40 function keystoke(obj){ //var e ...

  6. Unable to load native-hadoop library for your platform

    #HADOOP VARIABLES START export JAVA_HOME=/home/yang/jdk1.7.0_80export HADOOP_HOME=/home/hadoop/hadoo ...

  7. .Net Core + Angular2 环境搭建

    环境搭建: 1)node.js版本>5.0,NPM版本>3.0,TypeScript版本>2.0(全装最新版就好了) 2)安装NTVS 1.2(node tools for vs), ...

  8. C#动态添加属性

    class DynamicInputParams: DynamicObject { Dictionary<string, object> property = new Dictionary ...

  9. C#中MessageBox用法大全

    我们在程序中经常会用到MessageBox. MessageBox.Show()共有21中重载方法.现将其常见用法总结如下: 1.MessageBox.Show("Hello~~~~&quo ...

  10. php性能优化实用技巧:

    以下是五个优化技巧,熟练掌握后对于开发还是很有帮助的. 1. 对字符串使用单引号 PHP 引擎允许使用单引号和双引号来封装字符串变量,但是这个是有很大的差别的!使用双引号的字符串告诉 PHP 引擎首先 ...