【发布时间】:2016-03-04 12:31:37
【问题描述】:
我遇到了一个问题,即我需要为类中的 managed_shared_memory 成员使用 named_mutex,并得到“无法访问在类 boost::interprocess::named_mutex 中声明的私有成员”错误。但是,我都从 boost::noncpoyable 派生了我的类,并在构造函数中使用了带有 move-semantic 的 std::unique_ptr ,但没有成功。使用 boost 1_60 和 VS 2010,代码如下:
class FileLocker : private boost::noncopyable
{
public:
FileLocker();
~FileLocker();
private:
boost::interprocess::managed_shared_memory m_oShMem;
std::unique_ptr<boost::interprocess::named_mutex> m_oSetFileMutex;
};
cpp 文件:
FileLocker::FileLocker()
{
m_oShMem = managed_shared_memory(open_or_create, m_oMemName.c_str(), 1024);
m_oSetFileMutex = make_unique<named_mutex>( m_oShMem.find_or_construct<named_mutex>("viVideoFileInOutMutex")() );
}
最后是独一无二的:
template<typename T>
std::unique_ptr<T> make_unique()
{
return std::unique_ptr<T>( new T() );
}
template<typename T, typename Ts>
std::unique_ptr<T> make_unique(Ts&& params)
{
return std::unique_ptr<T>( new T(std::forward<Ts>(params)) );
}
我阅读了几个关于这个问题的 Stackoverflow-Threads,但他们都指出了我处理过的不可复制性......
感谢您的帮助!
【问题讨论】:
标签: c++ boost mutex private interprocess