简体   繁体   English

提高managed_shared_memory中的进程间互斥量

[英]boost interprocess mutex in managed_shared_memory

I have a thread in process 1 create a boost::interprocess::managed_shared_memory segment. 我在进程1中创建了一个线程:创建boost :: interprocess :: managed_shared_memory段。 In this segment I allocate a boost::interprocess::deque using a custom allocator and I create a boost::interprocess::interprocess_mutex and 2 boost::interprocess::interprocess_condition variables using the default allocator. 在此部分中,我使用自定义分配器分配boost :: interprocess :: deque,并使用默认分配器创建boost :: interprocess :: interprocess_mutex和2 boost :: interprocess :: interprocess_condition变量。 I use the find_or_construct method to create these. 我使用find_or_construct方法创建它们。

I have another process (process 2) which opens these using the find method on the boost::interprocess::managed_shared_memory segment which I have opened in process 2. 我有另一个进程(进程2),该进程使用在进程2中打开的boost :: interprocess :: managed_shared_memory段上的find方法打开它们。

I understand that managed_shared_memory segments have kernel or filesystem persistency and the interprocess_mutex/interprocess_condition variables have process level persistency. 我知道Managed_shared_memory段具有内核或文件系统持久性,而interprocess_mutex / interprocess_condition变量具有进程级别的持久性。

The scenario where I am getting stuck. 我陷入困境的场景。

1) Process 1 starts the thread which creates everything. 1)进程1启动创建所有内容的线程。

2) Process 2 starts and opens everything, at this stage the shared memory and synchronization is working well. 2)进程2启动并打开所有内容,在此阶段共享内存和同步运行良好。

3) Process 1 restarts the thread which tries to create everything again (I believe it shouldnt though as I am using find_or_construct) 3)进程1重新启动尝试再次创建所有内容的线程(我相信它不应该,因为我正在使用find_or_construct)

4) Process 2 is stuck on a wait call for a condition variable even though the thread in Process 1 has done a notify. 4)即使进程1中的线程已执行了通知,进程2仍在等待条件变量的等待调用上。

Am I missing something in terms of how I should create the shared memory and mutex/conditons or something along the lines of persistence? 在我应该如何创建共享内存和互斥体/条件方面,或者在持久性方面,我是否缺少某些东西? I am running this code on Windows. 我在Windows上运行此代码。

Consider using: 考虑使用:

boost::interprocess::named_mutex
boost::interprocess::scoped_lock<boost::interprocess::named_mutex>
boost::interprocess::named_condition 

Rather than allocating the mutexes & condition variables in an existing shared memory block. 而不是在现有的共享内存块中分配互斥体和条件变量。 Boost handles a lot of the messy details for you. Boost为您处理了许多混乱的细节。

Note: that you create these named_* objects in process space, not in shared memory. 注意:您在进程空间而不是共享内存中创建这些named_ *对象。 Boost creates the actual shared memory segments containing the mutex & condition variables for you. Boost将为您创建包含互斥量和条件变量的实际共享内存段。

I have also had trouble trying to map a shared memory segment twice into the same process. 我还很难尝试将共享内存段两次映射到同一进程中。 Is there any chance that when you run the second instance of the Process1 thread that is attempting to create a new mapping while the old one still exists? 当您运行Process1线程的第二个实例试图创建一个新的映射而旧的映射仍然存在时,是否有可能?

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

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