相关疑难解决方法(0)

我如何获得废弃的boost :: interprocess :: interprocess_mutex的所有权?

我的场景:一台服务器和一些客户端(虽然不多).服务器一次只能响应一个客户端,因此必须排队.我正在使用互斥(boost::interprocess::interprocess_mutex)执行此操作,包含在boost::interprocess::scoped_lock.

问题是,如果一个客户端在持有互斥锁时意外死亡(即没有析构函数运行),则其他客户端遇到麻烦,因为他们正在等待该互斥锁.我考虑过使用定时等待,所以如果我的客户端等待,比如20秒并且没有得到互斥锁,那么它就会继续与服务器通信.

这种方法的问题:1)它每次都这样做.如果它处于循环中,不断与服务器通信,则需要每次都等待超时.2)如果有三个客户端,并且其中一个客户端在持有互斥锁时死亡,则另外两个客户端将等待20秒并同时与服务器通信 - 这正是我试图避免的.

所以,我怎么能对客户说,"嘿那里,似乎这个互斥体已被抛弃,取得它的所有权"?

c++ concurrency synchronization boost boost-interprocess

7
推荐指数
1
解决办法
2545
查看次数

boost find in共享内存方法陷入c++多进程项目中

我正在使用 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)

c++ boost ipc shared-memory

6
推荐指数
1
解决办法
1142
查看次数