boost::interprocess——std::string 与 std::vector
我使用 boost::interprocess::managed_(windows_)shared_memory::construct 来构造一个持有自己的类的进程间向量,该向量有一个 std::string 类型的成员变量和另一个 std::vector 类型的成员变量,所以:
class myclass
{
public:
myclass()
{
}
std::string _mystring;
std::vector < int > _myintvector;
};
template < class _type >
struct typedefs
{
typedef boost::interprocess::managed_windows_shared_memory _memory;
typedef _memory::segment_manager _manager;
typedef boost::interprocess::allocator < _type, _manager > _allocator;
typedef boost::interprocess::vector < _type, _allocator > _vector;
};
typedef typedefs < myclass > tdmyclass;
int main ()
{
using namespace boost::interprocess;
managed_windows_shared_memory mem ( open_or_create, "mysharedmemory", 65536 );
tdmyclass::_vector * vec = mem.construct < tdmyclass::_vector > ( "mysharedvector" ) ( mem.get_segment_manager() );
myclass mytemp;
mytemp._mystring = "something";
mytemp._myintvector.push_back ( 100 );
mytemp._myintvector.push_back ( 200 );
vec->push_back ( mytemp );
/* waiting for the memory to be read is not what this is about,
so just imagine the programm stops here until everything we want to do is done */
}
我只是这样做是为了测试,我预计 std::string 和 std::vector 都不起作用,但是,如果我从另一个进程读入它,std::string 实际上起作用,它包含我分配的字符串。这真的让我很惊讶。 另一侧的 std::vector 仅部分工作, size() 返回的值是正确的,但如果我想访问迭代器或使用运算符[],程序就会崩溃。
所以,我的问题是,为什么会这样?我的意思是,我从未真正阅读过 Visual Studio SDK 的 STL 实现,但 std::string 不只是一个带有适合字符串的额外函数的 std::vector 吗?难道它们都使用 std::allocator - 这意味着 std::string 和 std::vector 都不能在共享内存中工作吗?
谷歌搜索并没有真正得到任何结果,除了 boost::interprocess::vector ,而这不是我搜索的内容。所以我希望有人能给我一些关于发生了什么的细节^^
PS:如果我在上面的代码中犯了一个打字错误,请原谅我,我刚刚在这个页面编辑器中写了它,我有点太习惯自动完成了我的IDE ^^
I used boost::interprocess::managed_(windows_)shared_memory::construct to construct an interprocess vector holding an own class, which has a member variable of type std::string and another of type std::vector, so:
class myclass
{
public:
myclass()
{
}
std::string _mystring;
std::vector < int > _myintvector;
};
template < class _type >
struct typedefs
{
typedef boost::interprocess::managed_windows_shared_memory _memory;
typedef _memory::segment_manager _manager;
typedef boost::interprocess::allocator < _type, _manager > _allocator;
typedef boost::interprocess::vector < _type, _allocator > _vector;
};
typedef typedefs < myclass > tdmyclass;
int main ()
{
using namespace boost::interprocess;
managed_windows_shared_memory mem ( open_or_create, "mysharedmemory", 65536 );
tdmyclass::_vector * vec = mem.construct < tdmyclass::_vector > ( "mysharedvector" ) ( mem.get_segment_manager() );
myclass mytemp;
mytemp._mystring = "something";
mytemp._myintvector.push_back ( 100 );
mytemp._myintvector.push_back ( 200 );
vec->push_back ( mytemp );
/* waiting for the memory to be read is not what this is about,
so just imagine the programm stops here until everything we want to do is done */
}
I just did this for testing, I expected neither std::string nor std::vector to be working, yet, if I read it in from another process, std::string actually works, it contains the string I assigned. That really surprised me.
The std::vector on the other side only partially works, the value returned by size() is correct, but the programm crashes if I want to access an iterator or by using operator[].
So, my question is, why is it that way ? I mean, I never actually read the STL implementations of the SDK of Visual Studio, but isn't std::string just an std::vector with extra functions suited for strings ? Don't they both use std::allocator - which would mean, that BOTH std::string and std::vector wouldn't work in shared memory ?
Googling for this doesn't really result into anything but boost::interprocess::vector, and thats not what I searched. So I hope someone can give me some details about whats going on ^^
PS: If I did a typo in the above code, please pardon me, I just wrote it right now in this pages editor and im kinda way too used to autocomplete of my IDE ^^
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
std::string
之所以有效,是因为您的标准库实现使用小字符串优化 (SSO)。这意味着字符串"something"
的值被复制到字符串对象本身,而不进行任何动态内存分配。因此您可以从其他进程中读取它。对于较长的字符串(尝试 30 个字符),它将不起作用。std::vector
不起作用,因为标准不允许它使用 SSO。它在第一个进程的地址空间中分配内存,但其他进程无法访问该内存。.size()
之所以有效,是因为它作为成员存储在向量本身的内部。PS
string
和vector
之间有很多区别。最重要也是最少提及的是string
从标准的角度来看,不是容器。std::string
works because your standard library implementation uses small-string-optimization (SSO). That means that the value of the string"something"
is copied into the string object itself, without any dynamic memory allocations. Hence you can read it from the other process. For longer strings (try 30 characters) it won't work.std::vector
doesn't work because it's not allowed to use SSO by standard. It allocates the memory in the address space of the first process, but this memory is not accessible by the other process. The.size()
works because it's stored inside the vector itself, as a member.P.S. there are many differences between
string
andvector
. The most important one, and the least mentioned, is thatstring
is not a container from standard's point of view.