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. jQuery深层次复制对象

    <script type="text/javascript" src="http://libs.baidu.com/jquery/1.9.1/jquery.min. ...

  2. Mvc api HelpPage 与注释

    一.添加包Microsoft.AspNet.WebApi.HelpPage可以自动给api生成帮助页面,url:/help 二.help加注释: 1. 2. public static class H ...

  3. css样式多个类、标签用【逗号 空格 冒号 点】分开的解析

    一:#a,b{…………}  id叫a和一个标签是b的样式(平行关系) 二:#a b{…………}  id叫a下面的一个标签是b的样式(包含关系) 三:#a.b{…………}  id叫a的下面的class叫 ...

  4. C#生成图片缩略图(2种思路)

    前言:在日常图片浏览中,如果图片过多,只有一张张的打开图片才能知道图片的内容,显然这样浏览起来非常不便.Windows系统在浏览图片时提供了缩略图的功能,这样大大的方便了浏览者了解每张图片的内容,本实 ...

  5. 如何解决Selenium中"Cannot find function addEventListener in object [object HTMLDocument]"的错误

    project: blog target: how-to-resolve-cannot-find-function-addEventListener-error-in-selenium.md stat ...

  6. (转) 浅析HTML5在移动应用开发中的使用

    (转)浅析HTML5在移动应用开发中的使用 (原)http://www.iteye.com/magazines/67   2012-03-07  来自 UECD.163.com  编辑 wangguo ...

  7. android基础(六)android的消息处理机制

    Android中的消息处理机制由四个部分组成:Message.Handler.MessageQueue和Looper,并且MessageQueue封装在Looper中,我们一般不直接与MQ打交道. 一 ...

  8. android中所有颜色大全

    < ?xml version="1.0" encoding="utf-8" ?>       < resources>< colo ...

  9. Bat文件, Services

    创建Bat文件自启动Windows Service; Services: sc stop/delete/query/find "service name"; Service Fol ...

  10. Nginx反向代理配置可跨域

    由于业务需要,同一项目中的前端代码放在静态环境中,而后端代码放在tomcat中,但此时问题却出现了:前端使用ajax请求后端获取数据时出现如下报错 XMLHttpRequest cannot load ...