0

I am trying to implement the following: I have to send and receive the same message but with different protocol stacks. For example, I have an MQTT client, which sends and receieves MQTT messages and ROS client, which does exactly the same but uses different messaging protocol to MQTT. Therefore, I have thought that using the Singleton class called Client which will hold the universal (primitive) variables like int x and this variable can be shared with ROSClient and MQTTClient. So I have read this answer as an example LINK and understood of using the static instance.

But I have two questions:

  1. How should I create an instance of a Singleton class Client? Can I create it in both ROSClient and MQTTClient or should I create in once in the main and just pass it to their respective functions?
  2. When the answer mentions "thread safe", does the author mean that the variables of the Singleton class cannot be mutated at once and I do not have to use std::atomic for example?
aikhsa
  • 1
  • *"should I create in once in the `main` and just pass it to their respective functions"* if you do that, you no longer need singleton :) which is good. – Jarod42 Nov 06 '19 at 10:15
  • @Jarod42 But I would prefer not to pass :) – aikhsa Nov 06 '19 at 10:17
  • But I am unsure you need singleton, both classes can use internally the same type and not necessary the same instance. – Jarod42 Nov 06 '19 at 10:22
  • @Jarod42 Yes but imagine that both instances need to modify that primitive value and on the other end only primitive value is used. So it would be easier to have some common primitive variable like `x` and it is used both to be mutated and read – aikhsa Nov 06 '19 at 10:27

0 Answers0