我正在使用 boost 的 ipc 库来保存复杂的对象,包括图像,在共享内存中,由多个进程使用。我们称这个对象为MyImage。共享内存是一个循环缓冲区,MyImage一次保存多个对象。
在我的代码中,有两个(或更多)进程写入共享内存中的一个段,另一个进程从中读取。此流程按预期工作,但是在读取器进程完成或崩溃后,当它尝试再次打开共享内存中的同一对象时,它会卡在find方法上,而写入器进程仍然运行良好。
我试图了解哪种竞争条件可能导致此问题,但在我的代码或 boost 的文档中找不到任何解释。
这是一个简单的代码,示例了我的项目中的问题:
流程Writer:
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/ipc/message_queue.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/circular_buffer.hpp>
using namespace std;
namespace bip = boost::interprocess;
static const char *const PLACE_SHM_NAME = "PlaceInShm";
static const char *const OBJECT_SHM_NAME = "ObjectInShm";
static const char *const PUSH_POP_LOCK = "push_pop_image_lock";
static const int IMAGES_IN_BUFFER = 20;
static const int OBJECT_SIZE_IN_SHM = 91243520;
class MyImage;
typedef bip::managed_shared_memory::segment_manager SegmentManagerType;
typedef bip::allocator<void, SegmentManagerType> MyImageVoidAllocator;
typedef bip::deleter<MyImage, SegmentManagerType> MyImageDeleter; …Run Code Online (Sandbox Code Playgroud)