I have several static variables in my dialog class that I’m declaring in the header and then defining immediately in the cpp file. They are being used in an application with 2 threads, and these particular variables are mostly shared between both threads.
- Main thread that runs the dialog box and manages various connections (data objects, menus, a USB device connection, and a shared memory connection)
- A “high priority” thread that reads USB input, runs shared memory commands, and updates one of the core data objects (a
This code is currently working with no issues, but it feels to me that it is unsafe, incorrect, or in a poor location.
Here’s the declaration of most of them from the header file:
static std::shared_ptr<CSysLatData> m_pOperatingSLD; vector<std::shared_ptr<CSysLatData>> m_vpPreviousSLD; static constexpr const char* m_caSysLat = "SysLat"; static constexpr const char* m_caSysLatStats = "SysLatStats"; static DWORD m_sysLatOwnedSlot;//UGH - I'm specifcally making the sysLatClient object thread local... but then to get a value from it I need to make a static var in this class to track it. Seems dumb. static CString m_updateString; static CString m_strError; static CString m_strBlack; static CString m_strWhite; static DWORD m_AppArraySize;
And here’s where I define them at the top of the cpp file.
//Define static variables - these should probably be done as inline or something... inlining is supposed to be available in C++17 and above, but Visual Studio throws a fit when I try to inline these. //also, I should probably move most of them to be global variables and NOT member variables CString CSysLat_SoftwareDlg::m_strStatus = ""; unsigned int CSysLat_SoftwareDlg::m_LoopCounterRefresh = 0; unsigned int CSysLat_SoftwareDlg::m_loopSize = 0xFFFFFFFF; CString CSysLat_SoftwareDlg::m_updateString = ""; CString CSysLat_SoftwareDlg::m_strError = ""; std::shared_ptr<CSysLatData> CSysLat_SoftwareDlg::m_pOperatingSLD = std::make_shared<CSysLatData>(); //does this need to be a unique_ptr? CString CSysLat_SoftwareDlg::m_strBlack = "<C=000000><B=10,10><C>"; CString CSysLat_SoftwareDlg::m_strWhite = "<C=FFFFFF><B=10,10><C>"; DWORD CSysLat_SoftwareDlg::m_sysLatOwnedSlot = 0; DWORD CSysLat_SoftwareDlg::m_AppArraySize = 0;
Full files if interested: