Boost.interprocess Translations-Quick Start

Source: Internet
Author: User
Tags diff

Quick Guide for the impatient Getting Started Guide Using shared memory as a pool of unnamed memory blocks Creating named shared memory Objects Using an offset smart pointer For GKFX memory Creating vectors in shared memory Creating maps in shared memories used as a pool of nameless ram blocks (a-pool of unnamed memories Blocks) Create a named Shared Memory object (named Shared memory objects) use the smart offset pointer (offset smart pointer) on shared RAM to create a vectors on shared memory to create maps Us on shared memory ing shared memory as a pool of unnamed memory blocks using shared ram as an unnamed block of RAM pools (a pool of unnamed memories blocks)

You can just allocate a portion of a GKFX memory segment, copy the message to this buffer, send the offset of that Porti On the shared memory to another process, and you is done. Let's see the example:

You just need to allocate a chunk of the shared memory segment, copy the message there, and send the offset address of that block in shared memory to another process, which is all you need to do. Let's take a look at the example:

#include <boost/interprocess/managed_shared_memory.hpp> #include <cstdlib>//std::system #include <
   sstream> int main (int argc, char *argv[]) {using namespace boost::interprocess; if (argc = = 1) {//parent process//Parent processes//remove shared memory on construction and destruction//in constructors and
         Delete the shared memory struct Shm_remove {shm_remove () {shared_memory_object::remove ("mysharedmemory") in the destructor;}
      ~shm_remove () {shared_memory_object::remove ("mysharedmemory");}

      } remover; Create a managed Shared memory segment//Creating a shared Segment Managed_shared_memory segment (Create_only, "mysharedmemory

      ", 65536); Allocate a portion of the segment (raw memory)//Request a segment in shared memory std::size_t free_memory = Segment.get_free_mem
      Ory ();

      void * Shptr = segment.allocate (1024/*bytes to allocate*/); Check invariant//checks if the size of the free space changes if (Free_memory <= segment.get_free_memory ()) return 1; An handle from the base address can identify any byte of the shared//memory segment even if it's mapped in diff
      Erent base addresses//Even if the shared memory is mapped to a different address space, the handle obtained through the//base address can identify any byte in the shared memory segment.
      managed_shared_memory::handle_t handle = segment.get_handle_from_address (SHPTR);
      Std::stringstream s;
      S << argv[0] << "" << handle;
      s << std::ends;
      Launch Child process//start subprocess if (0! = Std::system (S.str (). C_STR ())) return 1; Check memory has been freed//checks if the handle is released as a fruit process//The free memory size should be equal to Free_memory if (free_memory! = Segment.get
   _free_memory ()) return 1; } else{//open managed segment//Open the managed Shared Memory Segment Managed_shared_memory segment (Open_only, "mysharedmemory

      "); An handle from the base address can identify any byte of the shared//memory segment even if it's mapped in diff 
    Erent base addresses//even if shared memory is mapped to a different address space, this pass  The handle obtained by the base address can identify any byte in the shared memory segment.

      managed_shared_memory::handle_t handle = 0; Obtain handle value//Get handle values Std::stringstream s; s << argv[1];

      s >> handle;

      Get buffer local address from handle//by handle to get buffers for the native addresses of void *msg = Segment.get_address_from_handle (handle);
   Deallocate previously allocated memory//release of the previous application for Segment.deallocate (msg);
} return 0;
 }

Creating named shared memory Objects Create a named Shared Memory object (named Shared Memories Objects)

You want to the Create objects in a shared memory segment, giving a string name to them so and any other process can find, US E and delete them from the segment when the objects is not needed anymore. Example:

You can create an object on a shared memory segment and assign a string name to the object so that other processes can be searched by name, used, and deleted when they are no longer needed. Example:

#include <boost/interprocess/managed_shared_memory.hpp> #include <cstdlib>//std::system #include < cstddef> #include <cassert> #include <utility> int main (int argc, char *argv[]) {using namespace boost
   :: interprocess;

   typedef std::p air<double, int> MyType; if (argc = = 1) {//parent process//Parent processes//remove shared memory on construction and destruction//in constructors and
         Delete the shared memory struct Shm_remove {shm_remove () {shared_memory_object::remove ("mysharedmemory") in the destructor;}
      ~shm_remove () {shared_memory_object::remove ("mysharedmemory");}

      } remover; Construct managed Shared memory//fabric managed share (Managed) managed_shared_memory segment (create_onl

      Y, "mysharedmemory", 65536); Create an objects of MyType initialized to {0.0, 0}//Creates an object of type MyType and initializes it to {0.0, 0} MyType *instance = Segme Nt.construct<mytype> ("MyType instance")//name of the object
         (0.0, 0); ctor first argument//create an array of ten elements of MyType initialized to {0.0, 0}//10 MyType types of elements created Group, and initialized to {0.0, 0} MyType *array = segment.construct<mytype> ("MyType array")//name of the object objects                  Name [10]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.