class ugcs::vsm::Shared_memory¶
Overview¶
Platform independent implementation of system-wide named shared memory used for interprocess communications. More…
#include <shared_memory.h> class Shared_memory: public std::enable_shared_from_this< Shared_memory > { public: // typedefs typedef std::shared_ptr<Shared_memory> Ptr; typedef std::weak_ptr<Shared_memory> Weak_ptr; // enums enum Open_result; // methods template <typename... Args> static Ptr Create(Args&&... args); static bool Delete(const std::string& name); static Ptr Create(); virtual Open_result Open(const std::string& name, const size_t size) = 0; virtual void Close() = 0; virtual void* Get(); };
Detailed Documentation¶
Platform independent implementation of system-wide named shared memory used for interprocess communications.
Workflow: 1) Call Shared_memory::Create() to create class instance. 2) Call Shared_memory::Open() to open or create shared memory. 3) Call Shared_memory::Get() to Get the pointer to shared memory.
The pointer is valid until object is closed. Object is closed:
User must be aware of the following platform specific differences in shared memory semantics: Windows: 1) When the last handle to memory is closed the OS destroys the memory. (Note: OS closes all open handles on application termination/crash) 2) By default the memory is created in Session Kernel object namespace. 3) Shared memory cannot be explicitly deleted. Implemented via CreateFileMapping() and friends.
Linux: 1) Shared memory persists in the system until explicitly deleted or system restarted. 2) It is accessible from any session of its creator user. Implemented via shm_open() and friends.
Typedefs¶
typedef std::shared_ptr<Shared_memory> Ptr
Pointer type.
typedef std::weak_ptr<Shared_memory> Weak_ptr
Pointer type.
Methods¶
template <typename... Args> static Ptr Create(Args&&... args)
Create an instance.
static bool Delete(const std::string& name)
Deletes the named memory.
(Linux-only) Does not affect any opened memory with this name.
Parameters:
name |
Shared memory name. |
Returns:
true Shared memory deleted. false error occurred.
static Ptr Create()
Creates Platform specific class instance.
This does not create the memory itself, you should call Open() to create the named memory.
Returns:
shared_ptr<Shared_memory> of newly created instance
virtual Open_result Open(const std::string& name, const size_t size) = 0
Open/create shared memory.
Closes previously opened memory if any.
Parameters:
name |
Name of memory object |
size |
size in bytes. Will return error if size==0 |
Returns:
OPEN_RESULT_OK Opened existing shared memory. OPEN_RESULT_CREATED Created new shared memory. OPEN_RESULT_ERROR Error while creating shared memory. Previously opened memory is closed.
virtual void Close() = 0
Closes previously opened memory.
virtual void* Get()
Returns the pointer to shared memory.
Returns:
pointer to shared memory. Returns nullptr if not opened.