C ++ boost :: interprocess simple application

I want to write a simple application with boost that passes a string object to another process. It compiles well, but when I try to print a line from the second process, the following messages are put into the console and the second process crashes:

../boost_1_44_0/pulse/interprocess/synchronization/POSIX/interprocess_recursive_mutex.hpp: 107: invalid increase :: interprocess :: interprocess_recursive_mutex :: unlock (): The statement `res == 0 'failed.

first process code:

shared_memory_object::remove(SHARED_MEMORY_NAME); managed_shared_memory mshm(create_only, SHARED_MEMORY_NAME, SHARED_MEMORY_SIZE ); mshm.construct<string>( IP_STRING_NAME )("Message to other process"); string syscall(argv[0]); std::system( (syscall+" &").c_str() ); //starting second process 

second process code:

 managed_shared_memory mshm( open_or_create, SHARED_MEMORY_NAME, SHARED_MEMORY_SIZE ); std::pair<string * , size_t > p= mshm.find<string>(IP_STRING_NAME); cout<<"string is "<<*p.first<<endl; 

How can I make the application work correctly?

+4
source share
1 answer

It’s not clear from your code whether boost :: interprocess :: string or std :: string made sense, but from my few hours boost :: interprocess (pretty unpleasant ...) experience, you don’t want ...

So here a

Quick guide for strings in boost :: interprocess

First you need to define a special line:

 typedef boost::interprocess::allocator<char, boost::interprocess::managed_shared_memory::segment_manager> CharAllocator; typedef boost::interprocess::basic_string<char, std::char_traits<char>, CharAllocator> my_string; 

Secondly, when sending the application, you should use:

 // (mshm is the managed_shared_memory instance from the question) mshm.construct<my_string>( SOME_STRINGY_NAME )( "Message to other process", mshm.get_segment_manager()); 

Finally, the reader application should:

 std::pair<my_string * , size_t > p= mshm.find<my_string>(SOME_STRINGY_NAME); cout<< "got " << p.second << " strings " << endl; cout<< "first string is->"<<p.first->c_str()<<endl; 

Note. The reason for all this complexity is this .

Greetings

+6
source

All Articles