This is a classic case and should be something you learn to watch for in any multithreaded application.
You have multiple variables (x, y) that must be in a consistent state (when x != 0, y must have the correct value) in order for the relationship between them to be correctly interpreted.
The only way to ensure this state is to use some form of mutual exclusion around the Setter and the Tester so that both variables are looked at as a single atomic item.
As Philippe said, it might work on some platforms and some compilers but if you are ever to take your multithreaded programming skills to other platforms then you need to learn how to avoid cases like this in the general case and use the tools in the multithreaded API to control access.
I am not sure if you understood my post. I did answer that I would use mutex to synchrnonize and avoid wrong results. But, his question was explain how I can get the wrong result in this case and this had nothing to do with synchronization. He wanted me to see if I understood "memory fence" which I clearly never heard of to not allow instructions to be out of order while compiler optimization.
Like already pointed out, not declaring x and y as volatile could be a problem: the compiler might optimize away the test on x, i. e. pull it outside the loop, or it may just load x into a register and never bother to reload it from memory. In fact, since x is not declared as volatile, the compiler may feel obliged to make sure x is never reloaded from memory!
This is quite difficult to read. Please edit your message and replace the <code> tags by <pre> tags for correct code block formatting. you could also help by marking the line of code where your error occurs.
The error occur in PreSubclassWindow() function on line(I cant tell you exact Line Number) BOOL what=cid->Create(WS_VISIBLE|WS_CHILD|WS_BORDER,CRect(0,0,100,24),this,1);.
But here is some confusing issue. I have used PreSubClassWindow instead of onCreate Window message. And I also get some example that is created based on PreSubClassWindow. They failed at the same point. i.e. creating child window.
Later I changed it to onCreate and it solved my problems.
Hi, I seem to remember trying this in the past, though failed miserably (presumably for the same reason) I recall using a frame based window, since I could define the background image (as a HBRUSH) in the windows class (ms class, not c++ class)
Anyhow, I just ad a few fresh ideas and seem to have resolved the issue. I'll admit I didn't try with a bmp, but since trying to paint a solid color resulted in the same behaviour, I'll (perhaps foolishly?) assume that the solution would be valid if I had.
[EDIT: yup - it works just fine with a bitmap too]
It's just a simple matter of calling InvalidateRect along with the hWnd of the dialog, the client area (or NULL for all of it) and False - for do not redraw.
I.e, just add the line
InvalidateRect(hwndDlg, NULL, false);
after you've done the clean-up and before you return true.
I've been trying to write into the registry some data not related to the application in case.
As it happens CWinApp functions are not intended for such usage -- they are intended to write the registration data of the running application. In that case the correct usage is as follows:
1. Place call to the SetRegistryKey function in the "InitInstance". For example,
If you used the application wizard to start your application building, and you specified that you were building an MFC application... then you should already have a CWinApp derived class somewhere in your project. Don't redefine the call, just call it from somewhere in there (if you redefine you'll override the original call).
And you can only the CAux::SetRegistryKey(); method without initializing an object if the call is static (something you don't want to do anyway).
Sorry, it doesn't work either.
I do have a CWinApp derived class in my project (refer to my previous reply).
It is a simple dialog application. I need to write to the registry some value
and -- before using WriteProfileString -- I try to call SetRegistryKey.
There is the code snippet:
I'd tried various approaches until I found an excellent C++ Tutorial (http://www.learncpp.com/) and got the understanding that my problem is related to the inheritance subject. As it is clearly stated in the tutorial:
2. The protected access specifier allows derived classes to directly access members of the baseclasswhile not exposing those members to the public.
I see now that I have to make call within but my derived class. It does work except that there reveals another problem -- WriteRegisterString needs an access to the m_pszRegistryKey variable which is an attribute of CWinApp class. IAE, I have to learn a bit more on the subject.
The discussion was very helpful and I thank you for the time you spent on it.
Trying to convert a very old C program to Microsoft Visual Studio 2010. After some warnings, including some extern "C"'s and all that, everyting compiles. However, the linker keeps calling that is misses an external symbol "_VERSION". I have been looking through all code and can't find any reference to it. I included a char * _VERSION somewhere in my main source (within and outside the extern "C") Still complains.
Does anyone have an idea how I could find out where the linker finds the call of this reference? Is there som option that can persuade the linker to provide me with much more information or something like that?
This is getting extremely frustrating, since everything else seems to work as expected. Just this @!$@! _VERSION....