mirror of
https://github.com/boostorg/interprocess.git
synced 2026-01-25 06:12:25 +00:00
First complete documentation version
[SVN r34817]
This commit is contained in:
42
doc/code/doc_shared_memory.cpp
Normal file
42
doc/code/doc_shared_memory.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <boost/interprocess/shared_memory.hpp>
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
|
||||
int main ()
|
||||
{
|
||||
using namespace boost::interprocess;
|
||||
try{
|
||||
//Erase previous shared memory
|
||||
shared_memory_object::remove("shared_memory");
|
||||
|
||||
//Create a shared memory object.
|
||||
shared_memory_object shm
|
||||
(create_only //only create
|
||||
,"shared_memory" //name
|
||||
,memory_mappable::read_write //read-write mode
|
||||
);
|
||||
|
||||
//Set size
|
||||
shm.truncate(1000);
|
||||
|
||||
//Map the whole shared memory in this process
|
||||
mapped_region region
|
||||
(shm //What to map
|
||||
,mapped_region::read_write //Map it as read-write
|
||||
);
|
||||
|
||||
//Get the address of the mapped region
|
||||
void * addr = region.get_address();
|
||||
std::size_t size = region.get_size();
|
||||
|
||||
//Write all the memory to 1
|
||||
std::memset(addr, 1, size);
|
||||
|
||||
}
|
||||
catch(interprocess_exception &ex){
|
||||
std::cout << ex.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user