-
Nov 9th, 2021, 04:59 PM
#1
Thread Starter
Frenzied Member
How do I make MSVC++ optimization not ruin my program?
I'm running the MS VC++ compiler from the command line, and am compiling a C (not C++) program. When I perform maximum optimization with the command line switch /Ox (or even the simple speed optimization /O2) I find that what it's doing is getting rid of IMPORTANT variables that it THINKS are unused. But they aren't unused. They are being set in one thread, and then a call to CreateThread is making a new thread, where they are then used, and the CreateThread arg called lpParameter points to that variable so that the separate thread can use it (whatever is pushed in the lpParameter of CreateThread, appears the one and only arg for the thread proc itself). The problem is that vars pushed to callbacks this way aren't working properly. In the external thread, which is supposed to check content of this variable (it's one way that the main thread communicates with the second thread), the code that checks the variable is present in the normal version of the program, but this same variable-check code is missing in the optimized version. It seems to think that the variable-check code is checking something that's unused, and completely skipping the placing of this code into the compiled program.
Is there some pragma or some other feature in MS VC++ to label certain variables, functions, or other things as untouchable by the optimizer, so that the optimizer won't mistakenly get rid of something that it shouldn't? I want to use the maximum possible speed optimizations, because this program is involved in audio processing, and that means it needs to run VERY FAST to keep up with the data rate. Maximum optimizations are likely to be needed here, especially if I want it to be able to run fast enough on somewhat older PCs that have slower CPUs, so compiling it with no optimizations, just so that the optimizer leaves critical variables alone, would be a bad idea.
-
Nov 9th, 2021, 06:02 PM
#2
Re: How do I make MSVC++ optimization not ruin my program?
-
Nov 10th, 2021, 04:41 AM
#3
Re: How do I make MSVC++ optimization not ruin my program?
All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
Nov 14th, 2021, 07:17 AM
#4
Thread Starter
Frenzied Member
Re: How do I make MSVC++ optimization not ruin my program?
Originally Posted by wqweto
According to that article "in C and C++ it does not work in most threading scenarios, and that use is discouraged". So I don't know if C and C++ have an alternative way of doing it with threading, but it seems that I shouldn't be using volatile for that particular use. Any suggestions?
-
Nov 14th, 2021, 07:25 AM
#5
Re: How do I make MSVC++ optimization not ruin my program?
Originally Posted by Ben321
Any suggestions?
My suggestion is to test it out if it fixes the issues at hand, as long as it's not a major change to the source code.
The problem with volatile is that the variables are not thread-safe (you'll need atomic for this), not that volatile does not force the compiler to stop optimizing virables use (like using registers only with no memory storage).
cheers,
</wqw>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|