SharedApplication: Linux Shared Memory Wrapper in C++, Lockfree Queue

Introduction

SharedApplication is a class used for sharing data between processes, support malloc between multi-process and could be derived to create multi-process data structure.

SharedQueue is a lockfree queue with fixed capacity, derived from the SharedApplication. Easy to use.

Code

Github:https://github.com/mhy12345/SharedApplication

Usage

SharedApplication

Create a SharedApplication instance with a unique APPKEY. The APPKEY is used to tell the operating system which Shared Memory you are going to alloc.

Then, we can do some basic configuration to our application.

Set the size of the memory you are going to alloc, and this must called before start. If it wasn’t called, then the default size will be 1024 bytes.

Some other configurations are also available.

After the configurations, you can start the sApp, simple write:

Then, in you program, you can use malloc to alloc memory.

In other process, if you use the same APPKEY, and use same order of malloc, and same size, then the contents in the memory allocated should be synchronized.

Shared Queue

The SharedQueue is a private inherited form the SharedApplication, means you can just replace SharedApplication with SharedQueue.

In the main function, you can use  sQueue like a normal queue.

Attentions

  • In the SharedApplication, make sure you alloc each memory block using the same order and size
  • The destructive function must be called, otherwise the shared memory cannot be deleted.

发表评论

电子邮件地址不会被公开。 必填项已用*标注