Thread-safe subscriber count #1621
Replies: 2 comments 3 replies
-
Of course right after typing this up I figured out there is a subscription callback, which makes keeping track externally quiet convenient. Really thought I looked everywhere. Sorry! But hopefully this can be a pointer in the right direction for others. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Nothing in the library is thread safe, except for Loop::defer but it will never run immediately and it is costly. So you really want to segment/isolate connections to one single thread and keep them there. |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hej!
This is a really nice project, really happy to have found it!
I'm trying to figure out a decent way to get the number of subscribers to a topic in a thread-safe way. I have a program that basically runs in a loop and on every iteration it sends out some data to clients. It has fairly hard realtime requirements on limited embedded hardware. Calculating what to send is non-trivial and if we the client (usually only 1 or 2 clients per device) is not requesting that data we don't want to send it.
Getting the number of subscribers is easy with:
_app->topicTree->lookupTopic("topic")->size();
But that's not safe is it? The topic might be de-allocated at any time, due to unsubscribe.
I might use defer to execute this but then there is no great way to get the answer. Just setting some atomic variable might be fine but then I also have to deal with the fact that defer might not immediately execute the lambda.
I could also keep track of subscribe, unsubscribe and close calls and count the number of subscribers but that seems a bit convoluted for information that is already sort of known.
Any suggestions? Have I missed some simple solution?
Beta Was this translation helpful? Give feedback.
All reactions