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流总结 一. 流的分类 • 按数据流动方向 – 输入流:只能从中读取字节数据,而不能向其写出数据 – 输出流:只能向其写入字节数据,而不能从中读取数据 • 按照流所处理的数据类型 – 字节流: ...
随机推荐
- Counting Bits(Difficulty: Medium)
题目: Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate ...
- windows系统c盘占满/linux系统磁盘block、inode占满处理
windows系统 下载c盘清理.bat到服务器,双击bat文件将自动清理 linux系统 先远程ssh登录上服务器,登录教程:http://www.west263.com/faq/list.asp? ...
- LeetCode Rotate List
struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; class Solution ...
- C++关于Condition Variable
#include <condition_variable> #include <mutex> #include <future> #include <iost ...
- ios创建自定义控件必须具备的三个方法
1.当用代码创建控件时调用 -(instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { ...
- HDU 5015
http://acm.hdu.edu.cn/showproblem.php?pid=5015 矩阵是表示状态转移的利器 这题m很大,n非常小,所以开始的思考角度是能否从当前列推出下一列.有了这个角度, ...
- IOS 更改百度地图的定位图片
使用了百度地图的SDK,定位图片一直是蓝色的小圆点,很不喜欢,想换成自定义的图片,在网上搜罗了一大通,找到了解决的方案. 写下如下代码: //定位图层自定义样式参数 BMKLocationViewDi ...
- web前端性能14条规则
14条规则 1.减少Http请求 使用图片地图 使用CSS Sprites 合并JS和CSS文件 这个是由于浏览器对同一个host有并行下载的限制,http请求越多,总体下载速度越慢 2.使用CDN( ...
- B - Dividing
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Status Descr ...
- 如何用github快速搭建个人博客
当当当当-来看下新鲜出炉的Github博客 http://wli12.github.io/ 喜欢写markdown,但cnblogs对md文件的渲染简直丑爆了... 好奇怎么用github+Jekyl ...