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:
- How should I create an instance of a Singleton class
Client
? Can I create it in bothROSClient
andMQTTClient
or should I create in once in the main and just pass it to their respective functions? - 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?