Results 1 to 5 of 5

Thread: How do I make MSVC++ optimization not ruin my program?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,181

    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.

  2. #2

  3. #3
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    996

    Re: How do I make MSVC++ optimization not ruin my program?

    Also don't use CreateThread(). Use _beginthreadex()
    https://docs.microsoft.com/en-us/cpp...?view=msvc-170
    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)

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,181

    Re: How do I make MSVC++ optimization not ruin my program?

    Quote Originally Posted by wqweto View Post
    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?

  5. #5
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: How do I make MSVC++ optimization not ruin my program?

    Quote Originally Posted by Ben321 View Post
    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
  •  



Click Here to Expand Forum to Full Width