Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Where is a mutex needed/not needed? #50

Closed
mikelnrd opened this issue Jan 24, 2017 · 2 comments
Closed

Where is a mutex needed/not needed? #50

mikelnrd opened this issue Jan 24, 2017 · 2 comments
Assignees
Labels

Comments

@mikelnrd
Copy link

Hi

I'm a beginner in C. I'm familiar with nats from go, but am hoping someone can answer the following question for me...

The question:
If I have a single nats connection called 'nc', and I have multiple publishers and multiple subscribers all in the same thread... do I need to protect anything with mutex locking?

In case it's relevant:

  • the subscribers would be using natsSubscription_NextMsg with a timeout of 0 as I want a non-blocking subscription.
  • both my publishers and subscribers would be publishing/subscribing to various subjects

Thanks a lot for any help

@kozlovic
Copy link
Member

kozlovic commented Jan 24, 2017

@MikeLeonard Thank you for your interest in NATS!

All calls use internal locking where needed. As a user, you would need to do your own locking if you were to share the same callback with different subscribers (since the callback would be invoked from different threads for each subscriber). Note that this is true for any kind of callback that exist in C NATS. For instance, connection or error handlers, etc.. if you specify the same callback to different objects you take the risk that the code in that callback may be executed from different internal threads.

Since you are planning on using synchronous subscriptions, then you should not need any user-level locking. Note regarding natsSubscription_NextMsg with a timeout of 0: it means that if a message is already available in the internal queue, then you will get the message, otherwise you will get NATS_TIMEOUT.

Hope this helps!

@kozlovic kozlovic self-assigned this Jan 24, 2017
@kozlovic
Copy link
Member

kozlovic commented Feb 9, 2017

Closing this issue since no more question/issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants