问题
I have a Dll (C++) who contains a data_seg that is used to share variables among others programs. It works and many program is using it (30+). The problem is that I added this library into a new project, but the variable that I try to access, never change its value. I have to restart the program and now its synchronized with the rest of the other programs and I can see the last value of a variable. Any clue?
Thank you.
回答1:
You dont provide much information - like how you declare your variables or how you modify them. I assume you have written it properly as in MSDN documentation:
https://msdn.microsoft.com/en-us/library/h90dkhs0%28v=vs.90%29.aspx?f=255&MSPPError=-2147217396
From your description - that variables are synchronized only after application restart, I can only suspect you have some caching problems. I suggest you make your variables volatile and use atomics to modify/read them.
for example:
#pragma data_seg("Shared")
volatile LONG g_mydata = 0;
#pragma data_seg()
#pragma comment(linker, "/Section:Shared,RWS")
now to modify g_mydata (increment by 1):
InterlockedExchangeAdd((PLONG)&g_mydata, 1);
来源:https://stackoverflow.com/questions/36795484/dll-and-shared-variable