multithreading - Reader/Writer Locks in C++
You Only Need To Note This: only 1 single thread can acquire an upgrade_lock at one time.
|
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(){
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++的更多相关文章
- RFID 读写器 Reader Writer Cloner
RFID读写器的工作原理 RFID的数据采集以读写器为主导,RFID读写器是一种通过无线通信,实现对标签识别和内存数据的读出和写入操作的装置. 读写器又称为阅读器或读头(Reader).查询器(Int ...
- Stream,Reader/Writer,Buffered的区别(1)
Stream: 是字节流形式,exe文件,图片,视频等.支持8位的字符,用于 ASCII 字符和二进制数据. Reader/Writer: 是字符流,文本文件,XML,txt等,用于16位字符,也就是 ...
- 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 ...
- RubyMine生成reader/writer方法
RubyMine生成reader/writer方法 在非类的ruby文件中,Alt+Insert会出现新建文件的选项: 在ruby文件的类中,Alt+Insert会出现get/set方法生成提示和重构 ...
- Stream,Reader/Writer,Buffered的区别(2)
Reader: Reader的子类: 1.BufferedReader: FileReader 没有提供读取文本行的功能,BufferedReader能够指定缓冲区大小,包装了read方法高效读取字符 ...
- ExtJS4.2学习(7)——基础知识之Reader&Writer篇
Reader: 主要用于将proxy数据代理读取的数据按照不同的规则进行解析,将解析好的数据保存到Modle中. 结构图 Ext.data.reader.Reader 读取器的根类(很少直接实例化这个 ...
- 02_IO操作的基本规律(InputStream,OutputStream,Reader,Writer,FileReader,FileWriter,BufferedReader,BufferedWri
模拟BufferedInputStream,编写一个类 package toto.IO; import java.io.IOException; import java.io.InputStre ...
- Python CSV Reader/Writer 例子--转载
CSV(comma-separated values) 是跨多种形式导入导出数据的标准格式,比如 MySQL.Excel. 它以纯文本存储数和文本.文件的每一行就代表一条数据,每条记录包含了由逗号分隔 ...
- Java I/O流-总结(InputStream,OutputStream,Reader,Writer)
Java流总结 一. 流的分类 • 按数据流动方向 – 输入流:只能从中读取字节数据,而不能向其写出数据 – 输出流:只能向其写入字节数据,而不能从中读取数据 • 按照流所处理的数据类型 – 字节流: ...
随机推荐
- hibernate中validate的使用(转)
原文链接:http://blog.csdn.net/xing_sky/article/details/8484551 首先是要加入下面两个包 hibernate-validator-4.1.0.Fin ...
- Webdriver设置Chrome属性
1. ChromeDriver加载插件 File file = new File ("files\\youtube.crx"); ChromeOptions options = n ...
- K均值聚类算法的MATLAB实现
1.K-均值聚类法的概述 之前在参加数学建模的过程中用到过这种聚类方法,但是当时只是简单知道了在matlab中如何调用工具箱进行聚类,并不是特别清楚它的原理.最近因为在学模式识别,又重新接触了这 ...
- iOS学习之观察者模式
观察者模式: 观察者具体应用有两个:通知机制(notification)和KVO(key-value-observing)机制 通知机制: 谁要监听值的变化,谁就注册通知 ,特别要注意,通知的接受者必 ...
- MySQL数据库5 - 插入数据,修改数据,删除数据
一.插入数据 1. 所有列都插入值 INSERT [INTO] TABLE_NAME VALUES(V1,V2....Vn); 特点:列值同数,列值同序 eg: insert into users v ...
- Caliburn.Micro学习笔记目录——Zhouyongh
解析Caliburn.Micro(一) 解析Caliburn.Micro(二) 解析Caliburn.Micro(三) 解析Caliburn.Micro(四) Illusion = Caliburn. ...
- 数据结构《20》----Immutable stack
有趣的函数式数据结构<一>----不可变栈 什么是不可变?往栈中插入一个元素,原来的栈保持不变,返回一个新的栈(已插入新的元素). push, pop,getMax 等操作都要求在 常数时 ...
- 感知机的对偶形式——python3实现
运用对偶的(对应原始)感知机算法实现线性分类. 参考书目:<统计学习方法>(李航) 算法原理: 代码实现: 环境:win7 32bit + Anaconda3 +spyder 和原始算法的 ...
- cron表达式详解
@Scheduled(cron = "* * * * * *") cron表达式详解 1.cron表达式格式: {秒数} {分钟} {小时} {日期} {月份} {星期} {年份( ...
- meta头部标签意义作用!
1.<meta name="viewport" id="viewport" content="width=device-width, initi ...