#include <iostream>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/thread/thread.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
using namespace boost::interprocess;
int main() {
typedef boost::interprocess::allocator<char, boost::interprocess::managed_shared_memory::segment_manager> ShmemAllocator;
typedef basic_string<char, std::char_traits<char>, ShmemAllocator> BString;
typedef std::pair<BString *, std::size_t> BStringPair;
int count = 0;
shared_memory_object::remove("Highscore");
while (true) {
managed_shared_memory segment(open_or_create, "Highscore", 65536);
if (segment.get_num_named_objects() > 0){
BStringPair p = segment.find<BString> ("str");
printf("stuff in memory\n");
if (p.first ){
// THIS IS WHERE IT CRASHES!!!
//printf("%s \n",(p.first->c_str()) );
//int strlen = p.first->size();
}
printf("p.second: %d \n", p.second);
}
shared_memory_object::remove("Highscore");
//printf("one: %s", one.first);
//printf("two: %s", two.first);
int msecs = 2000;
boost::this_thread::sleep(boost::posix_time::milliseconds(msecs));
printf("sleeping...\n");
}
return 0;
}
build.sh for the reader
g++ -m32 -fPIC -Xlinker -zmuldefs strreader.cc -o strreader -I/common/COTS/boost_1_42_0/include -L/common/COTS/boost_1_42_0/lib -lboost_thread -lboost_system -pthread
strwriter.cpp
#include <iostream>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/thread/thread.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
using namespace boost::interprocess;
int main() {
typedef boost::interprocess::allocator<char, boost::interprocess::managed_shared_memory::segment_manager> ShmemAllocator;
typedef boost::interprocess::basic_string<char, std::char_traits<char>, ShmemAllocator> BString;
try {
shared_memory_object::remove("Highscore");
printf("cleared out memory\n");
managed_shared_memory segment(open_or_create, "Highscore", 65536);
printf("initialized managed shared memory \n");
ShmemAllocator allocInst (segment.get_segment_manager());
BString bstrData(segment.get_segment_manager());
bstrData = "hello";
std::string data = "Hello";
//BString bstrToSend(data.begin(), data.end());
//BString bstrToSend(data.c_str());
BString *mystring = segment.construct<BString> ("str")(bstrData);
//mystring="Hello";
printf("inserted mystring into shmvector \n");
} catch (boost::interprocess::bad_alloc e) {
printf("we caught bad alloc\n");
printf("explanation: %s", e.what());
throw;
} catch (...) {
printf("caught unknown exception\n");
throw;
}
printf("wrote number");
return 0;
}
build.sh for the writer:
g++ -m32 -fPIC -Xlinker -zmuldefs strwriter.cpp -o strwriter -I/common/COTS/boost_1_42_0/include -L/common/COTS/boost_1_42_0/lib -lboost_thread -lboost_system -pthread