mirror of
https://github.com/boostorg/interprocess.git
synced 2026-01-26 06:32:20 +00:00
First complete documentation version
[SVN r34817]
This commit is contained in:
43
doc/code/doc_shared_memory2.cpp
Normal file
43
doc/code/doc_shared_memory2.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#include <boost/interprocess/shared_memory.hpp>
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
|
||||
int main ()
|
||||
{
|
||||
using namespace boost::interprocess;
|
||||
try{
|
||||
//Open already created shared memory object.
|
||||
shared_memory_object shm
|
||||
(open_only //only create
|
||||
,"shared_memory" //name
|
||||
,memory_mappable::read_only //read-write mode
|
||||
);
|
||||
|
||||
//Map the whole shared memory in this process
|
||||
mapped_region region
|
||||
(shm //What to map
|
||||
,mapped_region::read_only //Map it as read-write
|
||||
);
|
||||
|
||||
//Get the address of the mapped region
|
||||
void * addr = region.get_address();
|
||||
std::size_t size = region.get_size();
|
||||
|
||||
//Check that memory was initialized to 1
|
||||
const char *mem = static_cast<char*>(addr);
|
||||
for(std::size_t i = 0; i < size; ++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;
|
||||
}
|
||||
Reference in New Issue
Block a user