Code Monkey home page Code Monkey logo

shared-resource-ipc's Introduction

Shared Resource IPC

Wrapper for a resource that can be shared across processes.

Supported Operating Systems

  • Linux
  • MacOS
  • Windows

Linux/MacOS support is achieved using POSIX shared memory and semaphores.

Windows support is desired, but not planned any time soon.

Usage

In your Cargo.toml file, add the line:

shared-resource-ipc = "0.1"

Here is a basic example of what can be done with this package.

use shared_resource_ipc::SharedResource;
use serde::{Serialize, Deserialize};

#[derive(Clone, Default, Serialize, Deserialize, Default)]
struct DemoStruct {
    number: usize,
    name: String,
}

fn main() {
    let default_value: DemoStruct = DemoStruct::default();

    // Open a shared resource using a unique identifier and a default value. If this process
    // is not the first to initialize the resource, the default value is ignored.
    let shared_resource: SharedResource<DemoStruct> = 
        SharedResource::new("unique_name", default_value)
        .expect("failed to open shared resource");

    // Access the stored value using a clojure.
    let _result = shared_resource.access(|data: &DemoStruct| {
        // read the data here...
        let value = data.clone();
        // a value can be returned from the clojure
        return value;
    }).expect("failed to access shared resource");

    // The value can be accessed mutably as well.
    let _result = shared_resource.access(|data| {
        (*data).number = 42;
    }).expect("failed to mutably access shared resource");

    // No cleaning is required: it is handled when the resource is dropped.
}

Possible Issue

This implementation of a shared resource does not know how many processes connect to the memory segment.

This means that sometimes, a process will create, open and completely destroy the memory before another process has the change to access it. The program will panic most times when this happens. However, it won't panic if a second process re creates the memory not knowing it was already destroyed before. As far as that second process is concerned, it is the first one to create the resource so it will not panic.

Solution: make sure your resource has a certain goal to accomplish. As such, a condition can be tested to then drop the resource in a process.

Alternatively, a delay can be added before the resource drops, but this will work less predictably.

If you know of any way to solve this problem, please open an issue.

shared-resource-ipc's People

Contributors

krisnk avatar

Watchers

 avatar

shared-resource-ipc's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.