2009-08-13 64 views
3

我有一個簡單的結構:升壓進程間:共享內存和STL類型

struct MyType 
{ 
    std::string name; 
    std::string description; 
} 

,我把它在一個共享內存:

managed_shared_memory sharedMemory(open_or_create, "name", 65535); 
MyType* pType = sharedMemory.construct<MyType>("myType")(); 
// ... setting pType members ... 

如果兩個應用程序共享通信內存是使用不同版本的Visual Studio(不同版本的stl實現)構建的,我應該將本機類型放在共享內存中(例如char *)而不是stl類型?

編輯

我試着用

typedef boost::interprocess::basic_string<char> shared_string; 

和它的作品!

回答

2

Boost.Interprocess通常提供STL類型的替代品,以便在共享內存中使用。 std :: string,特別是當只有一個結構體的成員時,將不能從另一個進程訪問。其他人也有such a problem

+1

呀,請參閱本節文檔:http://www.boost.org/doc/libs/1_39_0/doc/html/interprocess/allocators_containers.html#interprocess。 allocators_containers.containers_explained – 2009-08-13 13:58:12

3

您應該使用

typedef boost::interprocess::basic_string<char> shared_string; 
struct MyType 
{ 
    shared_string name; 
    shared_string description; 
}