Giter VIP home page Giter VIP logo

Comments (2)

kozlovic avatar kozlovic commented on June 15, 2024

@hidai It does not make sense to me to be calling nats_Open and/or nats_Close concurrently.

Think about it. You would have code running trying to open the library, do some NATS operations, while another part of your code would be closing it? What does that say about the application?

Adding a lock in the library would mean that the lock would need to survive the close, which then would be reported as memory in use (which some will report as memory leak, which is not). So if need be I will then document that these should not be called concurrently.

from nats.c.

hidai avatar hidai commented on June 15, 2024

Now I think that my problem should be addressed not by the library side but by the user side.
But let me explain what I wanted to do here since this might be useful for other users who have similar use-case with me.
The problem I was trying to deal with is Case 3 in the figure below.

Case 1

client A      A--------->|
client B            B----------->|
opened?       |<---------------->|
              ^                  ^
              |                  |
              nats_Open          nats_Close

Case 2

client A      A--------->|
client B                      B----------->|
opened?       |<-------->|    |<---------->|
              ^          ^    ^            ^
              |          |    |            |
              nats_Open  |    nats_Open    |
                         nats_Close        nats_Close

Case 3

client A      A--------->|
client B                 B----------->|
opened?       |<-------->|<---------->|
              ^          ^
              |          |
              nats_Open  !!race condition!!
                         nats_Close and nats_Open are called concurrently

The race condition can be properly handled by user side using an integer counter and a mutex like below.

class Nats {
public:
    Nats() {
        std::lock_guard<std::mutex> lock(m);
        nats_Open(-1);
        ++c;
    }

    ~Nats() {
        std::lock_guard<std::mutex> lock(m);
        if (c == 1) {
            nats_Close();
        }
        --c;
    }

private:
    static int c = 0;
    static std::mutex m;
};

from nats.c.

Related Issues (20)

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.