简体   繁体   English

如果其他进程可以对其进行迭代,如何安全地清除共享内存中的boost :: interprocess:vector?

[英]How can I safely clear a boost::interprocess:vector in shared memory if other processes could be iterating over it?

Let's say I have a boost interprocess vector in shared memory. 假设我在共享内存中有一个boost进程间向量。 One process may be iterating through it. 一个过程可能正在遍历它。 If another process wants to concurrently clear the vector, what do I need to do so that I don't crash in the process that is doing the iteration? 如果另一个进程想要同时清除向量,我该怎么做才能使我不会在进行迭代的进程中崩溃?

PROCESS #1 - iterates over vector in shared memory 流程1-遍历共享内存中的向量

using namespace boost::interprocess;

typedef allocator<int, managed_shared_memory::segment_manager>  ShmemAllocator;
typedef vector<int, ShmemAllocator> MyVector;

managed_shared_memory segment(create_only, "MySharedMemory", 65536);
const ShmemAllocator alloc_inst (segment.get_segment_manager());
MyVector *myvector = segment.construct<MyVector>("MyVector")(alloc_inst);
size_t size = myvector->size();
for (size_t i = 0; i < size; i++)
{
  //potential crash when PROCESS 2 calls clear
  int value = myvector->at(i);
}

PROCESS #2 - can potentially clear the vector in shared memory 过程2-可能会清除共享内存中的向量

using namespace boost::interprocess;

typedef allocator<int, managed_shared_memory::segment_manager>  ShmemAllocator;
typedef vector<int, ShmemAllocator> MyVector;

managed_shared_memory segment(create_only, "MySharedMemory", 65536);
const ShmemAllocator alloc_inst (segment.get_segment_manager());
MyVector *myvector = segment.construct<MyVector>("MyVector")(alloc_inst);
if (somecondition)
   myvector->clear();

Two things I was thinking: copy the vector to local memory std::vector before iterating. 我在想两件事:在迭代之前将向量复制到本地内存std :: vector。 I'm not sure if the copy would be safe, though, either. 不过,我不确定该副本是否安全。 Can't imagine it would 无法想象会

or 要么

some sort of interprocess mutex, which for simplicity's sake, I'd like to avoid. 某种进程间互斥,为了简单起见,我想避免这种情况。

If you're concurrently modifying and accessing it, then you'll need a mutex or equivalent. 如果要同时修改和访问它,则需要一个互斥锁或等效项。

Copying (or moving) it, then iterating over the non-shared copy might be sensible, if the iteration is slow - you'll only need to lock the mutex for the copy/move, not the iteration. 复制(或移动)它,然后遍历非共享副本可能是明智的,如果迭代速度很慢-您只需要锁定互斥对象即可进行复制/移动,而无需迭代。 But you'll still need to guard all accesses to the shared object with a mutex. 但是,您仍然需要使用互斥量保护对共享库的所有访问。

By definition, a shared memory segment can be accessed by multiple writers/readers. 根据定义,共享的内存段可以由多个写入器/读取器访问。 Consequently, you MUST protect the data and serialize all access to the segment. 因此,您必须保护数据并序列化对该网段的所有访问。 I do not think you can avoid using some sort of mutex. 我认为您不能避免使用某种互斥量。 The way I have done this, is to defined a header struct at the top of the segment and have all shared data start after that struct. 我这样做的方法是在该段的顶部定义标头结构,并在该结构之后开始所有共享数据。 In that struct you can put any house-keeping elements like flags, etc. But the most important is to put a mutex at the top of the struct. 在该结构中,您可以放置​​任何内部维护元素,例如标志等。但是最重要的是在该结构的顶部放置一个互斥体。 Then the write/read access routines acquire the mutex first before writing or reading the data. 然后,写入/读取访问例程在写入或读取数据之前先获取互斥量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM