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.
shared_application :: SharedApplication<APPKEY> sApp;
Then, we can do some basic configuration to our application.
sApp.setSize(size_t size);
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.
void setCPU(); //Atomatically bind a CPU void setCPU(int id); //Bind a CPU[id]
After the configurations, you can start the sApp, simple write:
sApp.start();
Then, in you program, you can use malloc to alloc memory.
void* ptr = sApp.malloc(size_t size);
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.
shared_application :: SharedQueue<T,capacity,APPKEY> sQueue; sQueue::init();//called first in the main function
In the main function, you can use sQueue like a normal queue.
sQueue::push(T &val); sQueue::pop(T &val); sQueue::full(T &val); sQueue::empty(T &val);
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.