使用boost的时候。源码查看后发现boost进程通讯中使用命名队列。boost会创建一个boost_interprocess的目录并且根据磁盘启动时间创建一个子目录并且创建一个命名文件。问题就出现这个启动时间里。在启动量一万+以上的情况下。有2-3百个崩溃出现在这里。
inline bool shared_memory_object::priv_open_or_create
(ipcdetail::create_enum_t type, const char *filename, mode_t mode, const permissions &perm)
{
m_filename = filename;
std::string shmfile;
ipcdetail::create_tmp_and_clean_old_and_get_filename(filename, shmfile);
//Set accesses
if (mode != read_write && mode != read_only){
error_info err = other_error;
throw interprocess_exception(err);
}
switch(type){
case ipcdetail::DoOpen:
m_handle = ipcdetail::open_existing_file(shmfile.c_str(), mode, true);
break;
case ipcdetail::DoCreate:
m_handle = ipcdetail::create_new_file(shmfile.c_str(), mode, perm, true);
break;
case ipcdetail::DoOpenOrCreate:
m_handle = ipcdetail::create_or_open_file(shmfile.c_str(), mode, perm, true);
break;
default:
{
error_info err = other_error;
throw interprocess_exception(err);
}
}
//Check for error
if(m_handle == ipcdetail::invalid_file()){
error_info err = system_error_code();
this->priv_close();
throw interprocess_exception(err);
}
m_mode = mode;
return true;
}
原因:主进程在boost_interprocess下根据磁盘时间戳创建目录并创建文件,子进程打开文件失败从而boost抛出异常,内部原因是boost通过wmi获取的时间变化了,导致打开文件的路径与创建文件路径不一致,从而打开文件失败。
测试:当我改变了系统时间的时候,再次运行程序。发现创建文件的目录是旧路径(旧时间戳),打开文件的目录是新路径(新的时间戳)