Files
interprocess/example/doc_windows_shared_memory2.cpp
2007-05-04 21:17:55 +00:00

45 lines
1.4 KiB
C++

//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztañaga 2006-2007. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/interprocess for documentation.
//
//////////////////////////////////////////////////////////////////////////////
//[doc_windows_shared_memory2
#include <boost/interprocess/windows_shared_memory.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <iostream>
#include <cstring>
int main ()
{
using namespace boost::interprocess;
try{
//Open already created shared memory object.
windows_shared_memory shm(open_only, "shared_memory", read_only);
//Map the whole shared memory in this process
mapped_region region (shm, read_only);
//Check that memory was initialized to 1
const char *mem = static_cast<char*>(region.get_address());
for(std::size_t i = 0; i < 1000; ++i){
if(*mem++ != 1){
std::cout << "Error checking memory!" << std::endl;
return 1;
}
}
std::cout << "Test successful!" << std::endl;
}
catch(interprocess_exception &ex){
std::cout << "Unexpected exception: " << ex.what() << std::endl;
return 1;
}
return 0;
}
//]