Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

c++ - Dll and shared variable

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...