有没有更好的方法来检查boost共享内存段的存在
Is there a better way to check for the existence of a boost shared memory segment?
我能看到如何做到这一点的唯一方法是尝试访问它,并捕获如果不存在就会抛出的异常。
bool exists()
{
using namespace boost::interprocess;
try
{
managed_shared_memory segment(open_only, kSharedMemorySegmentName);
return segment.check_sanity();
}
catch (const std::exception &ex) {
std::cout << "managed_shared_memory ex: " << ex.what();
}
return false;
}
有更好的方法吗?
我在玩boost::interprocess,碰巧问了同样的问题。我做了一点挖掘,最后确定了open_or_create来满足我的需求。然而,在助推的模板意大利面(我的是1.62)的深处,我们发现了这个宝石:
/*<... snip ...>*/
//This loop is very ugly, but brute force is sometimes better
//than diplomacy. If someone knows how to open or create a
//file and know if we have really created it or just open it
//drop me a e-mail!
bool completed = false;
spin_wait swait;
while(!completed){
try{
create_device<FileBased>(dev, id, size, perm, file_like_t());
created = true;
completed = true;
}
catch(interprocess_exception &ex){
if(ex.get_error_code() != already_exists_error){
throw;
}
else{
try{
DeviceAbstraction tmp(open_only, id, read_write);
dev.swap(tmp);
created = false;
completed = true;
}
catch(interprocess_exception &e){
if(e.get_error_code() != not_found_error){
throw;
}
}
catch(...){
throw;
}
}
}
catch(...){
throw;
}
swait.yield();
}
/* <... snip ...> */
以上内容来自managed_open_or_create_impl.hpp,大约第360行,在priva_open_or_create()方法中。答案似乎是否定的。不,在尝试打开共享内存之前,没有很好的方法来检查是否已经创建了共享内存。
如果使用shared_memory_object
s:
using namespace boost::interprocess;
bool exists()
{
try
{
shared_memory_object shm_source(open_only, map_shared_memory_name.c_str(), read_only);
std::string name = shm_source.get_name();
return (!name.empty());
}
catch (const std::exception &ex)
{
return (false);
}
}
相关文章:
- 使用Boost Interprocess创建托管共享内存需要很长时间
- 字符串共享内存映射的向量
- CUDA 使用共享内存平铺 3D 卷积实现
- 共享内存:MapViewOfFile 返回错误 5
- 如何在多写入器情况下对文件支持的共享内存中的大页面出错
- 有没有办法列出所有共享内存对象的名称?
- 共享内存的升压容器是否实现锁定?
- 共享内存中的健壮互斥锁不是那么健壮
- 使用IPC/共享内存将Integer数组从C++传递到Python
- 共享内存和性能
- 在这种特殊情况下,我是否需要在共享内存中使用原子类型
- 是否可以在专用内存空间中分配一个为提升管理共享内存而创建的对象
- fork(),在C中共享内存和指针
- 访问共享内存而不使用易失性、std::atomic、信号量、互斥锁和自旋锁
- 提升进程间共享内存open_or_create每次都会引发异常
- 通过 mmap-ed 共享内存传递可变长度 C 字符串
- 越界访问 CUDA 共享内存
- 在共享内存中插入映射映射时出现编译器错误
- 矩阵矢量产品 CUDA 通过平铺和共享内存提高性能
- 如何更改在 c++ 中使用提升库创建的共享内存的路径