- 浏览: 679490 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (181)
- Matlab (16)
- Java (26)
- autoHotkey (4)
- openCV (1)
- C++ (50)
- PDF-XChange Viewer (2)
- 学术 (11)
- javascript (4)
- linux (11)
- SWT (9)
- latex (7)
- GAE (2)
- windows (6)
- C# (3)
- PS (20)
- JNI (4)
- latex,批处理 (0)
- 批处理 (4)
- Android (5)
- 矩阵论 (1)
- SVM (1)
- 概率图模型 (0)
- Python (12)
- Eigen (5)
- 编程题 (1)
- MKL (1)
- 神经网络 (9)
- 最优化 (2)
- 摄影 (1)
- PPT (0)
- After Effects (3)
- CUDA (3)
- caffe (0)
- MXNet (2)
- svn (1)
- R (0)
- 虚拟机 (0)
- tensorflow (7)
- theano (1)
- Keras (1)
- vim (1)
- xgboost (1)
- spark (6)
- eclipse (2)
- word2vec (0)
- hadoop (1)
- dmlc (1)
- git (0)
最新评论
-
jeffersonz:
请问大神,这个插件记录下来的脚本为什么不能再Extendscr ...
PhotoShop - 记录PS的所有操作为JavaScript代码 -
cherishLC:
Andy__Zou 写道cherishLC 写道Andy__Z ...
Eigen的编译选项;MKL的使用 -
Andy__Zou:
cherishLC 写道Andy__Zou 写道cherish ...
Eigen的编译选项;MKL的使用 -
cherishLC:
Andy__Zou 写道cherishLC 写道Andy__Z ...
Eigen的编译选项;MKL的使用 -
Andy__Zou:
cherishLC 写道Andy__Zou 写道 您好,您的工 ...
Eigen的编译选项;MKL的使用
boost库中的interprocess包可以在共享内存中创建map等复杂类型的数据,
但是不能用std::map,主要原因时其中的指针用的绝对位置,而非相对的
用boost的map,vector等类型,必须显示的指定allocator,因而复杂了不少。
代码小而全的一个实现:http://blog.csdn.net/dx2880/article/details/7315761
我在学习的过程中参考了错误的代码,走了不少弯路。这里贴出的示例主要是将
创建共享内存 和 使用共享内存中的数据 两部分分离成为了两个程序,更符合一般的使用场景(但其实类定义应该引用一个文件):
注意:经测试,在创建共享内存的程序中修改Item类定义,比如在最后新加一个int tmp; 变量, 使用共享内存的程序在不该把Item类定义的情况下仍然可以正常运行(但是估计只适用于map,vector作为容器时不行)
老规矩,先看运行结果:
最后附上完整程序:
编译(注意需要修改boost库的路径)
创建共享内存 managed_shared_mem_use_map.cpp:
使用共享内存 managed_shared_mem_use_map__read_existing.cpp:
但是不能用std::map,主要原因时其中的指针用的绝对位置,而非相对的
用boost的map,vector等类型,必须显示的指定allocator,因而复杂了不少。
代码小而全的一个实现:http://blog.csdn.net/dx2880/article/details/7315761
我在学习的过程中参考了错误的代码,走了不少弯路。这里贴出的示例主要是将
创建共享内存 和 使用共享内存中的数据 两部分分离成为了两个程序,更符合一般的使用场景(但其实类定义应该引用一个文件):
注意:经测试,在创建共享内存的程序中修改Item类定义,比如在最后新加一个int tmp; 变量, 使用共享内存的程序在不该把Item类定义的情况下仍然可以正常运行(但是估计只适用于map,vector作为容器时不行)
老规矩,先看运行结果:
- 第一次运行创建共享内存的程序,可以正常创建;
- 第二次运行创建共享内存的程序,因为已经存在该名字的共享内存,所以抛出异常
- 第一次运行使用共享内存的程序,可以正常使用,并删除该共享内存
- 第一次运行使用共享内存的程序,因为已经删除了该共享内存,所以抛出异常
最后附上完整程序:
编译(注意需要修改boost库的路径)
boost_path="/home/colinliang/ThirdParty/boost_1_65_1" #注意, -lrt必须放到最后!!! 因为managed_shared_mem_use_map.cpp 要用到该库,,这个书写顺序会改变链接顺序!!! #参见 https://stackoverflow.com/questions/43052020/undefined-reference-to-shm-open-even-while-compiling-with-pthread-lrt #It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file foo.o but before bar.o. If bar.o refers to functions in ‘z’, those functions may not be loaded. g++ -std=c++0x -L/lib -I $boost_path managed_shared_mem_use_map.cpp -o exe_managed_shared_mem_use_map -pthread -lrt g++ -std=c++0x -L/lib -I $boost_path managed_shared_mem_use_map__read_existing.cpp -o exe_managed_shared_mem_use_map__read_existing -pthread -lrt
创建共享内存 managed_shared_mem_use_map.cpp:
/** 在共享内存中使用 boost 的map, ---创建共享内存 原始代码有个致命的错误——存储的Value中用来std::string!! 会导致segment fault * 代码来自: http://blog.csdn.net/nellson/article/details/50681103 * 官方文档: http://www.boost.org/doc/libs/1_65_1/doc/html/interprocess.html * 更复杂的例子,比如 map中嵌套vector, 以及 boost::unordered_map 的使用 等 请参见 http://www.boost.org/doc/libs/1_65_1/doc/html/interprocess/allocators_containers.html#interprocess.allocators_containers.containers_explained.containers_of_containers * 需要添加的库:https://stackoverflow.com/questions/7985236/c-boost-libraries-shared-memory-object-undefined-reference-to-shm-open * 链接时需要添加 rt 库,方法为 g++ -L /lib -lrt */ //#include <boost/interprocess/shared_memory_object.hpp> //#include <boost/interprocess/mapped_region.hpp> #include <boost/interprocess/managed_shared_memory.hpp> #include <boost/interprocess/allocators/allocator.hpp> #include <boost/interprocess/containers/map.hpp> #include <boost/interprocess/containers/vector.hpp> #include <boost/interprocess/containers/string.hpp> #include <functional> #include <cstdlib> #include <utility> #include <sstream> #include <stdio.h> #include <stdlib.h> #include <exception> #include <sys/mman.h> #include <sys/stat.h> #include <fcntl.h> #include <string> #include <iostream> using namespace boost::interprocess; //Typedefs of allocators and containers typedef managed_shared_memory::segment_manager segment_manager_t; typedef allocator<void, segment_manager_t> void_allocator; typedef allocator<int, segment_manager_t> int_allocator; typedef vector<int, int_allocator> int_vector; typedef allocator<int_vector, segment_manager_t> int_vector_allocator; typedef vector<int_vector, int_vector_allocator> int_vector_vector; typedef allocator<char, segment_manager_t> char_allocator; typedef basic_string<char, std::char_traits<char>, char_allocator> char_string; using std::string; class Item { public: Item(const void_allocator& alloc, const char* name = "", int id = 0, int size = 0) : id(id), size(size), name(name, alloc) { } ~Item() { } int id; int size; char_string name; //注意所有需要动态分配内存的变量,都只能用boost 的allocator进行内存分配 }; typedef char_string KeyType; typedef Item MappedType; typedef std::pair<const char_string, Item> ValueType; typedef allocator<ValueType, managed_shared_memory::segment_manager> ShmemAllocator; typedef map<KeyType, MappedType, std::less<KeyType>, ShmemAllocator> MyMap; int main() { try { // init managed_shared_memory segment(create_only, "SharedMemory", 65536); //注意这里必须指定共享内存的大小 const void_allocator alloc_inst(segment.get_segment_manager()); MyMap * mymap = segment.construct < MyMap > ("MyMap")(std::less<KeyType>(), alloc_inst); MyMap * mymap2 = segment.construct < MyMap > ("MyMap2")(std::less<KeyType>(), alloc_inst); //可以构建两个,但名称不能一样,否则会产生异常boost::interprocess_exception::library_error KeyType key(alloc_inst); Item v(alloc_inst); for (int i = 0; i < 5; ++i) { key = (std::string("n") + std::to_string(i)).c_str(); v.id = i * 10; v.size = -i; v.name = (std::string("n") + std::to_string(i)).c_str(); mymap->insert(ValueType(key, v)); } MyMap* mymap_for_use = segment.find < MyMap > ("MyMap").first; long int r = segment.find < MyMap > ("MyMap").second; printf("result of find map in shared-memory: %ld\n", r); printf("key-values in shared memory: \n"); for (MyMap::iterator it = mymap_for_use->begin(); it != mymap_for_use->end(); it++) { const auto& item = it->second; printf("\tkey: %s, item.id: %d, item.size: %d, item.name: %s\n", it->first.c_str(), item.id, item.size, item.name.c_str()); } //shared_memory_object::remove("SharedMemory"); } catch (const std::exception & e) { printf("Exception:%s\n", e.what()); //shared_memory_object::remove("SharedMemory"); } return 0; }
使用共享内存 managed_shared_mem_use_map__read_existing.cpp:
/** 在共享内存中使用 boost 的map, ----使用已经存在的共享内存 原始代码有个致命的错误——存储的Value中用来std::string!! 会导致segment fault * 代码来自: http://blog.csdn.net/nellson/article/details/50681103 * 官方文档: http://www.boost.org/doc/libs/1_65_1/doc/html/interprocess.html * 更复杂的例子,请参见http://www.boost.org/doc/libs/1_65_1/doc/html/interprocess/allocators_containers.html 中的“Containers of containers”一节 * 需要添加的库:https://stackoverflow.com/questions/7985236/c-boost-libraries-shared-memory-object-undefined-reference-to-shm-open * 链接时需要添加 rt 库,方法为 g++ -L /lib -lrt */ //#include <boost/interprocess/shared_memory_object.hpp> //#include <boost/interprocess/mapped_region.hpp> #include <boost/interprocess/managed_shared_memory.hpp> #include <boost/interprocess/allocators/allocator.hpp> #include <boost/interprocess/containers/map.hpp> #include <boost/interprocess/containers/vector.hpp> #include <boost/interprocess/containers/string.hpp> #include <functional> #include <cstdlib> #include <utility> #include <sstream> #include <stdio.h> #include <stdlib.h> #include <exception> #include <sys/mman.h> #include <sys/stat.h> #include <fcntl.h> #include <string> #include <iostream> using namespace boost::interprocess; //Typedefs of allocators and containers typedef managed_shared_memory::segment_manager segment_manager_t; typedef allocator<void, segment_manager_t> void_allocator; typedef allocator<int, segment_manager_t> int_allocator; typedef vector<int, int_allocator> int_vector; typedef allocator<int_vector, segment_manager_t> int_vector_allocator; typedef vector<int_vector, int_vector_allocator> int_vector_vector; typedef allocator<char, segment_manager_t> char_allocator; typedef basic_string<char, std::char_traits<char>, char_allocator> char_string; using std::string; class Item { public: Item(const void_allocator& alloc, const char* name = "", int id = 0, int size = 0) : id(id), size(size), name(name, alloc) { } ~Item() { } int id; int size; char_string name; //注意所有需要动态分配内存的变量,都只能用boost 的allocator进行内存分配 }; typedef char_string KeyType; typedef Item MappedType; typedef std::pair<const char_string, Item> ValueType; typedef allocator<ValueType, managed_shared_memory::segment_manager> ShmemAllocator; typedef map<KeyType, MappedType, std::less<KeyType>, ShmemAllocator> MyMap; int main() { try { //shared_memory_object::remove("SharedMemory"); // init managed_shared_memory segment(open_only, "SharedMemory"); MyMap* mymap_for_use = segment.find < MyMap > ("MyMap").first; long int r = segment.find < MyMap > ("MyMap").second; printf("result of find map in shared-memory: %ld\n", r); printf("key-values in shared memory: \n"); for (MyMap::iterator it = mymap_for_use->begin(); it != mymap_for_use->end(); it++) { const auto& item = it->second; printf("\tkey: %s, item.id: %d, item.size: %d, item.name: %s\n", it->first.c_str(), item.id, item.size, item.name.c_str()); } shared_memory_object::remove("SharedMemory"); } catch (const std::exception & e) { printf("Exception:%s\n", e.what()); shared_memory_object::remove("SharedMemory"); } return 0; }
发表评论
-
C++函数中的静态变量
2019-07-19 11:22 762在C++中,可以在成员函数中使用静态变量,从而间接达到在hpp ... -
tensorflow 静态编译笔记 -- linux系统
2018-11-05 14:26 0注: 1、编译tensorflow静态库 很简单,参见: h ... -
gperftools笔记
2018-06-27 14:30 0export LD_LIBRARY_PATH=LD_LIBR ... -
dmlc分布式线性模型编译笔记
2018-06-06 10:37 21051、使用第三方修改后的wormhole工程 原始工程wor ... -
C++中读取控制台输出,并将文件指针FILE*转换为istream
2017-11-27 15:03 1707解决的问题: 1、通过执行系统的bash命令后,获取其输出( ... -
近似最近邻算法库FLANN
2017-04-25 18:45 0FLANN库是计算近似最近邻的库, 问题定义如下: 给定距离函 ... -
C++杂记-- 重定向std::cout等
2017-04-18 17:28 18891、屏蔽std::cout 参见:http://stackov ... -
CMake指定目标代码的平台(win32,x64)
2016-06-03 20:54 11534主要参考自:http://blog.c ... -
windows 10下安装GPU版MXNet
2016-06-03 21:28 8528上回说到编译CPU版的MXNet: http://cheris ... -
windows下安装CPU版mxnet
2016-05-23 23:14 17487MXnet (mix net)类似Caffe,Tensorfl ... -
Linux C++编译及 静态/动态 链接库 笔记
2015-12-20 19:49 59861、C++文件的编译 g++参 ... -
Linux (Ubuntu) 下 Eclipse C++ 环境配置
2015-12-08 21:49 19201、安装JDK 参见http://blog.csdn.net/ ... -
Photoshop插件开发-filter
2015-10-11 22:53 3311初学,,感觉PS插件的文档很烂,,教程也很少,,也就这么简单学 ... -
使Visual Assist X支持CUDA的.cu文件的代码提示
2015-07-17 00:05 2227现在安装CUDA很方便了,直接支持Visual Studio。 ... -
CUDA程序block和thread超出硬件允许值时的异常
2015-07-13 11:32 3681调用CUDA的核函数时指定block 和 thread大小,该 ... -
创建及使用DLL
2014-09-29 15:22 1194此篇仅作简要的记录,作为备忘。 更为详细的可以参考:http: ... -
matlab的mexFunction中使用std::cout(部分转载)
2014-09-27 15:37 2124在matlab、C++混合编程的时候,可能会用到之前的代码。代 ... -
Eigen库:那些年我们一起经历的bug
2014-09-24 18:39 17701、使用auto声明新变量 ... -
C++:无虚函数的类作为有虚函数类的父类时产生Expression:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
2014-09-24 09:48 1899调程序时忘了为啥,写了一小段测试程序,再简单不过,结果跑出了E ... -
C++(MSVC) 下查看call strack trace;查看函数是否被inline
2014-09-20 15:08 1194最近写C++程序,,各种 ...
相关推荐
大部分boost库功能的使用只需包括相应头文件即可,少数(如正则表达式库,文件系统库等)需要链接库。里面有许多具有工业强度的库,如graph库。STL集中在算法部分,而boost包含了不少工具类,可以完成比较具体的工作...
在送审的C++标准库TR1中,有十个Boost库成为标准库的候选方案。在更新的TR2中,有更多的Boost库被加入到其中。从某种意义上来讲,Boost库成为具有实践意义的准标准库。 可下载Boost C++ Libraries安装boost库。大...
c++ 用的boost库,该库提供了强大的函数,很多可以作为基础的函数来使用。
c++boost库最新版本
C++ BOOST库 中文文档,下载免费
超越c++标准库boost程序库导论.rar
Boost库是一个经过千锤百炼、可移植、提供源代码的C++库,作为标准库的后备,是C++标准化进程的发动机之一。 Boost库由C++标准委员会库工作组成员发起,其中有些内容有望成为下一代C++标准库内容。 Boost中比较有...
boost中文手册C++ boost库让我们更快、更好地使用C++进行大型系统或者服务器开发工作,提高工作效率,很值得一看!
boost库下载,c++库,方便,高效,比c++标准程序库好用!
完整的电子书,中文版,是进行C++开发的必备三方库。
C++ Boost库学习记录-安装,详述了boost库德安装使用过程,方便快捷
Linux 下C++共享内存、信号量封装,实现进程同步
C++共享内存进程间通信 FileMapping用于进程间快速通信
超越c++标准库 - boost程序库导论
Boost程序库完全开发指南:深入C++“准”标准库+Boost程序库完全开发指南:深入C++“准”标准库+Boost程序库完全开发指南:深入C++“准”标准库+Boost程序库完全开发指南:深入C++“准”标准库+Boost程序库完全开发...
扫描完整版 Nginx模块开发指南.使用C++11和Boost程序库 Nginx模块开发指南.使用C++11和Boost程序库
超越C++标准库:Boost库导论 美)卡尔森.著.张杰
Boost项目(Boost C++ Libraries)是一个广泛使用的C++库集合,它提供了一系列广泛使用的任务和功能的实现,如智能指针、图形处理、数值分析、字符串和文本处理等。这些库几乎支持所有主流的操作系统,包括Windows、...
Boost_C++库,清晰中文,共16章,比较适合Boost库入门
共享内存,C++,Windows