| Home | Libraries | People | FAQ | More

## Class windows_shared_memory

boost::interprocess::windows_shared_memory

## Synopsis

    // In header: <boost/interprocess/windows_shared_memory.hpp>

    class windows_shared_memory {
    public:

      // public member functions
      windows_shared_memory() noexcept;
      windows_shared_memory(create_only_t, const char *, mode_t, std::size_t,
                            const permissions & = permissions());
      windows_shared_memory(open_or_create_t, const char *, mode_t, std::size_t,
                            const permissions & = permissions());
      windows_shared_memory(open_only_t, const char *, mode_t);
      windows_shared_memory(create_only_t, const wchar_t *, mode_t, std::size_t,
                            const permissions & = permissions());
      windows_shared_memory(open_or_create_t, const wchar_t *, mode_t,
                            std::size_t, const permissions & = permissions());
      windows_shared_memory(open_only_t, const wchar_t *, mode_t);
      windows_shared_memory(windows_shared_memory &&) noexcept;
      windows_shared_memory & operator=(windows_shared_memory &&) noexcept;
      void swap(windows_shared_memory &) noexcept;
      ~windows_shared_memory();
      const char * get_name() const noexcept;
      mode_t get_mode() const noexcept;
      mapping_handle_t get_mapping_handle() const noexcept;
      offset_t get_size() const noexcept;
    };

## Description

A class that wraps the native Windows shared memory that is implemented as a file mapping of the paging file. Unlike shared_memory_object, windows_shared_memory has no kernel persistence and the shared memory is destroyed when all processes destroy all their windows_shared_memory objects and mapped regions for the same shared memory or the processes end/crash.

Warning: Windows native shared memory and interprocess portable shared memory (boost::interprocess::shared_memory_object) can't communicate between them.

### `windows_shared_memory` public member functions

1.     windows_shared_memory() noexcept;

   Default constructor. Represents an empty windows_shared_memory.

2.     windows_shared_memory(create_only_t, const char * name, mode_t mode,
                             std::size_t size,
                             const permissions & perm = permissions());

   Creates a new native shared memory with name "name" and at least size "size", with the access mode "mode". If the file previously exists, throws an error.

3.     windows_shared_memory(open_or_create_t, const char * name, mode_t mode,
                             std::size_t size,
                             const permissions & perm = permissions());

   Tries to create a shared memory object with name "name" and at least size "size", with the access mode "mode". If the file previously exists, it tries to open it with mode "mode". Otherwise throws an error.

4.     windows_shared_memory(open_only_t, const char * name, mode_t mode);

   Tries to open a shared memory object with name "name", with the access mode "mode". If the file does not previously exist, it throws an error.

5.     windows_shared_memory(create_only_t, const wchar_t * name, mode_t mode,
                             std::size_t size,
                             const permissions & perm = permissions());

   Creates a new native shared memory with name "name" and at least size "size", with the access mode "mode". If the file previously exists, throws an error.

6.     windows_shared_memory(open_or_create_t, const wchar_t * name, mode_t mode,
                             std::size_t size,
                             const permissions & perm = permissions());

   Tries to create a shared memory object with name "name" and at least size "size", with the access mode "mode". If the file previously exists, it tries to open it with mode "mode". Otherwise throws an error.

7.     windows_shared_memory(open_only_t, const wchar_t * name, mode_t mode);

   Tries to open a shared memory object with name "name", with the access mode "mode". If the file does not previously exist, it throws an error.

8.     windows_shared_memory(windows_shared_memory && moved) noexcept;

   Moves the ownership of "moved"'s shared memory object to *this. After the call, "moved" does not represent any shared memory object. Does not throw

9.     windows_shared_memory & operator=(windows_shared_memory && moved) noexcept;

   Moves the ownership of "moved"'s shared memory to *this. After the call, "moved" does not represent any shared memory. Does not throw

10.     void swap(windows_shared_memory & other) noexcept;

    Swaps to shared_memory_objects. Does not throw.

11.     ~windows_shared_memory();

    Destroys *this. All mapped regions are still valid after destruction. When all mapped regions and windows_shared_memory objects referring the shared memory are destroyed, the operating system will destroy the shared memory.

12.     const char * get_name() const noexcept;

    Returns the name of the shared memory.

13.     mode_t get_mode() const noexcept;

    Returns access mode.

14.     mapping_handle_t get_mapping_handle() const noexcept;

    Returns the mapping handle. Never throws.

15.     offset_t get_size() const noexcept;

    Returns the size of the windows shared memory. It will be a 4K rounded size of the "size" passed in the constructor.