I'm trying to share a variable across multiple processes/threads, and make it so that if one process updates the value, all the other processes 'get' the new value.
I read that I could/should use mutex's/semaphores/interlocked* funcs/critical sections/events/file mappings, but I wasnt sure which or why. Heres the code Im using atm, is this a safe method of doing it?
Thanks in advance for any help.
Code:
#include <windows.h>
#include <tchar.h>
#pragma data_seg("SHARED")
int nSharedCount;
#pragma data_seg()
#pragma comment(linker, "/section:SHARED,RWS")
BOOL APIENTRY DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved)
{
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
{
}
break;
case DLL_PROCESS_DETACH:
{
}
break;
}
}
void GetCount()
{
return nSharedCount;
}
void SetCount(int nCount)
{
nSharedCount = nCount;
}