C++ boost named_条件不是唤醒等待进程

C++ boost named_条件不是唤醒等待进程,c++,boost,synchronization,conditional-statements,boost-interprocess,C++,Boost,Synchronization,Conditional Statements,Boost Interprocess,我有两个进程(生产者和消费者)在共享内存中共享一个int-deque,我让生产者进程在deque中放入两个数字,然后它进入等待状态,失去互斥锁。然后,我让消费者处理删除数字并打印它们。然后,它根据生产者正在等待的条件发出通知。然后,消费者在第二个条件下自行等待。在这种情况下,制作人没有醒来。我在进程之间使用相同的互斥。请在下面查找所有代码 包括文件共享内存。h: #ifndef SharedMemory_h #define SharedMemory_h #include <boost/i

我有两个进程(生产者和消费者)在共享内存中共享一个int-deque,我让生产者进程在deque中放入两个数字,然后它进入等待状态,失去互斥锁。然后,我让消费者处理删除数字并打印它们。然后,它根据生产者正在等待的条件发出通知。然后,消费者在第二个条件下自行等待。在这种情况下,制作人没有醒来。我在进程之间使用相同的互斥。请在下面查找所有代码

包括文件共享内存。h:

#ifndef SharedMemory_h
#define SharedMemory_h

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/deque.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/interprocess/offset_ptr.hpp>
#include <boost/interprocess/sync/named_condition.hpp>

using namespace boost::interprocess;

typedef allocator<offset_ptr<int>, managed_shared_memory::segment_manager> ShmemAllocator;
typedef deque<offset_ptr<int>, ShmemAllocator> Deque;

#endif 
\ifndef SharedMemory\u h
#定义SharedMemory\u h
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间boost::interprocess;
typedef分配器ShmemAllocator;
typedef deque deque;
#恩迪夫
生产过程:

#include "shared_memory.h"

struct shm_remove
{
    shm_remove() { shared_memory_object::remove("MySharedMemory"); }
    ~shm_remove() { shared_memory_object::remove("MySharedMemory"); }
} remover;

struct mutex_remove
{
    mutex_remove() { named_mutex::remove("MyMutex"); }
    ~mutex_remove() { named_mutex::remove("MyMutex"); }
} mutex_remover;

//Create shared memory, mutex and condtion
managed_shared_memory segment(create_only, "MySharedMemory", 10000000);
named_mutex mutex(create_only,"MyMutex");
named_condition full(open_or_create,"FullCondition");
named_condition empty(open_or_create,"EmptyCondition");

const ShmemAllocator alloc_inst (segment.get_segment_manager());


int main() 
{
    Deque* MyDeque;
    offset_ptr<int> a, b;
    try{
        MyDeque = segment.construct<Deque>("MyDeque")(alloc_inst);
        try{
           a = static_cast<int*> (segment.allocate(sizeof(int)));
           b = static_cast<int*> (segment.allocate(sizeof(int)));
        }catch(bad_alloc &ex){
             std::cout << "Could not allocate int" << std::endl;
        }
    }catch(bad_alloc &ex){
        std::cout << "Could not allocate queue" << std::endl;
    }
    scoped_lock<named_mutex> lock(mutex);
    while(1)
    {
        while (MyDeque->size() == 2)
        {
            full.wait(lock);
            std::cout << "unlocked producer" << std::endl;
        }

        if (MyDeque->size() == 0)
        {
            *a = 2;
            MyDeque->push_back(a);
        }

        if (MyDeque->size() == 1)
        {
            *b = 4;
            MyDeque->push_back(b);
            empty.notify_one();
        }
    }
}
#include "shared_memory.h"

managed_shared_memory segment(open_only, "MySharedMemory");
Deque* MyDeque = segment.find<Deque>("MyDeque").first;

named_mutex mutex(open_only, "MyMutex");
named_condition full(open_only, "FullCondition");
named_condition empty(open_only, "EmptyCondition");

int main()
{
    scoped_lock<named_mutex> lock(mutex);
    while(1)
    {

         //volatile int size = MyDeque->size();
         while (MyDeque->size() == 0)
         {
             empty.wait(lock);
         }

         if (MyDeque->size() == 2)
         {
             std::cout << "Consumer: " << *MyDeque->front() << std::endl;
             MyDeque->pop_front();
         }

         if (MyDeque->size() == 1)
         {
             std::cout << "Consumer: " << *MyDeque->front() << std::endl;
             MyDeque->pop_front();
             full.notify_one();
         }
    }
}
#包括“shared_memory.h”
结构shm_删除
{
shm_remove(){shared_memory_object::remove(“MySharedMemory”);}
~shm_remove(){shared_memory_object::remove(“MySharedMemory”);}
}去除剂;
结构互斥删除
{
mutex_remove(){named_mutex::remove(“MyMutex”);}
~mutex_remove(){named_mutex::remove(“MyMutex”);}
}互斥移除器;
//创建共享内存、互斥和条件
托管共享内存段(仅创建“MySharedMemory”,10000000);
命名为互斥互斥体(仅创建“MyMutex”);
命名为“完整条件”(打开或创建“完整条件”);
命名条件为空(打开或创建“空条件”);
const ShmemAllocator alloc_inst(段.get_段管理器());
int main()
{
德克*我的德克;
偏移量ptr a、b;
试一试{
MyDeque=段结构(“MyDeque”)(alloc_inst);
试一试{
a=静态_转换(段分配(sizeof(int));
b=静态_转换(段分配(sizeof(int));
}捕获(错误分配和释放){
std::cout size();
而(MyDeque->size()==0)
{
空。等待(锁定);
}
如果(MyDeque->size()==2)
{
std::cout size()==1)
{

std::cout不能在通知中锁定互斥体。这就是死锁的原因。

我不熟悉boost interprocess,但有没有理由认为共享内存中的互斥体在两个进程中都起作用?请明确指定所有同步对象都属于boost.interprocess(即
boost::进程间
namespace),因为Boost.Thread库中也存在具有不同功能的类似对象。我认为它的功能与您尝试获取命名互斥体时一样,如果该互斥体不可用,则会引发异常。此外,当我使用断点调试代码时,有时使用者尝试获取互斥体,但无法作为producer拥有它。只有当生产者等待并丢失互斥时,消费者才能获得它。我有一个使用名称空间boost::interprocess。我应该在代码中加入它。@rudasi我想你必须向我们展示“创建互斥体和条件”的代码