I am creating a system state class that needs be thread safe in reading/writing. I am implementing it using the Singleton Design Pattern. I have based it off of pointers from these Stackoverflow questions:
Here is a condensed example of my code. Essentially it is just some state variables I need to keep track of and share across several threads. I know for a fact there will only ever needs be one state instance and I know that it needs to thread safe and constant. Will this implementation ensure that?
// System_State.h
class System_State
{
public:
static System_State &getInstance()
{
static System_State stateInstance;
return stateInstance;
}
System_State(System_State const&) = delete;
void operator=(System_State const&) = delete;
// Example Setter with mutex guard
void setWifiConnectedStatus(uint8_t _status)
{
std::lock_guard<std::mutex> lock(mtx);
Wifi.ConnectedStatus = _status;
}
private:
System_State()
{
initializeState();
}
void initializeState();
std::mutex mtx;
static struct WifiService
{
uint8_t ConnectedStatus;
std::string CurrentConnection;
std::string SavedNetworks;
std::string AvailableNetworks;
std::string ForgetNetwork;
std::string ConnectTo;
std::string DisconnectFrom;
} Wifi;
static struct ScanService
{
std::string PerformScan;
std::string ScanStatus;
uint8_t ScanProgress;
} Scan;
static struct UploadDataService
{
std::string PerformUpload;
uint8_t UploadProgress;
uint8_t ItemsWaitingToBeUploaded;
} UploadData;
static std::string LEDControlBrightness;
static uint8_t BatteryPercentage;
};
And here is some example main
//main.cpp
#include <thread>
void loop1()
{
System_State state = System_State::getInstance();
while(true)
{
//do randomness with state
}
}
void loop2()
{
System_State state2 = System_State::getInstance();
while(true)
{
//do randomness with state2
}
}
int main()
{
std::thread t1(loop1);
std::thread t2(loop2);
// do and join and all that!
return 0;
}