mercredi 6 novembre 2019

How to correctly initialize the Singleton class instance and use its variables?

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?

Aucun commentaire:

Enregistrer un commentaire