Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8667993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:06:21+00:00 2026-06-12T18:06:21+00:00

I have a little problem with a C++ class… I have a private member

  • 0

I have a little problem with a C++ class…
I have a private member bool clientConnected.
and a getter bool isClientConnected() {return clientConnected;}

now the proble is.. that clientConnected is set from a different thread.

in main I have a loop

while (!x.isClientConnected())
{}

now if I compile it in Debug config.
All works fine… while exits as soon as clientConnected is set to true.
but if I compile it in Release config.
The compiler optimizes the loop as its constant.
and makes the following:

00141C01  cmp         al,bl  
00141C03  je          SDL_main+0A1h (141C01h)  

the value of al is never updated again.
so its always thinks its false.
I tried also with volatile, same results

how can I prevent this optimization in class so the value will get updated on each call, without having to write something like

bool z = x.isClientConnected();
while (!z) { z = x.isClientConnected(); }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:06:22+00:00Added an answer on June 12, 2026 at 6:06 pm

    volatile http://msdn.microsoft.com/en-us/library/12a04hfd(v=vs.80).aspx looks like simple answer to your problem:

    “Objects declared as volatile are not used in certain optimizations because their values can change at any time.”

    volatile bool clientConnected;
    bool isClientConnected() const volatile {return clientConnected;}
    

    But – you have loop taking CPU:

    while (!x.isClientConnected())
    {}
    

    Better is to wait on semaphore:

    x.waitForConnected();
    void X::waitForConnected()
    {
       WaitForSingleObject(m_connectedSemaphore, ,....);
    }
    

    How to create/use semaphore in MS Windows see: http://msdn.microsoft.com/en-us/library/windows/desktop/ms686946(v=vs.85).aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little problem. I have class private static class EfficientAdapter extends BaseAdapter
Hi all im using a sqlite helper class, but i have a little problem
I have a little Problem with the app that I'm developing. In the App
I have a little problem with a class I am currently writing a save
I have a little problem. Class ll: interface jj{ public class ll implements gg{
i have a little problem implementing some serialization/deserialization logic. I have several classes that
I have a little problem with the visited pseudo-class and the text-shadow property in
I have a little problem, i probably included the class files wrongly, since i
I have a little problem in object programming in javascript There is a class
i have a little problem with the media player class. I have a button,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.