Advertisement
piffy

Boost Interprocess

Jul 19th, 2021
996
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include <boost/interprocess/shared_memory_object.hpp>
  2. #include <boost/interprocess/mapped_region.hpp>
  3. #include <iostream>
  4.  
  5. //Compilare con g++ boostshm.cpp -lrt -lpthread
  6.  
  7.  
  8. using namespace boost::interprocess;
  9.  
  10. int main()
  11. {
  12.   shared_memory_object shdmem{open_or_create, "Boost", read_write};
  13.   shdmem.truncate(1024);
  14.   std::cout << shdmem.get_name() << '\n';
  15.   offset_t size;
  16.   if (shdmem.get_size(size))
  17.     std::cout << size << '\n';
  18.   mapped_region region{shdmem, read_write};
  19.   std::cout << std::hex << region.get_address() << '\n';
  20.   std::cout << std::dec << region.get_size() << '\n';
  21.   mapped_region region2{shdmem, read_only};
  22.   std::cout << std::hex << region2.get_address() << '\n';
  23.   std::cout << std::dec << region2.get_size() << '\n';
  24.   int *i1 = static_cast<int*>(region.get_address());
  25.   *i1 = 42;
  26.   int *i2 = static_cast<int*>(region2.get_address());
  27.   std::cout << *i2 << '\n';
  28.   bool removed = shared_memory_object::remove("Boost");
  29.   std::cout << std::boolalpha << removed << '\n';
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement