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 7555795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:42:10+00:00 2026-05-30T11:42:10+00:00

We have a wrapper that represents an atomic integer. Internally, it is implemented using

  • 0

We have a wrapper that represents an atomic integer. Internally, it is implemented using Windows’ InterlockedIncrement() and InterlockedDecrement() functions, which work on a volatile long variable:

class AtomicInt {
public:
    ...

    operator long() const { return m_value; }

private:
    volatile long m_value;
};

My question is, is the above operator for querying the current value correct (i.e. you always get the current value) or do I need to declare the method also as volatile to prevent any problems with cached values?

  • 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-05-30T11:42:12+00:00Added an answer on May 30, 2026 at 11:42 am

    What you’ve done is fine … you don’t need to mark the method as volatile. Also since you’ve included the definition of the method in the class declaration, the method will typically be implicitly inlined by the compiler, so the actual function call mechanism would be elided during the compiler optimization process. Thus in most contexts, the variable itself would be accessed directly without an intervening function call, making the volatile declaration on the variable enough.

    BTW, for future reference, keep in mind that in a multi-processor environment outside of x86, the volatile keyword is not enough to ensure that a thread using your shared memory value not receive cached data. The volatile keyword in general merely prevents certain compiler optimizations. On processor platforms with weaker memory consistency rules (i.e., ARM, PowerPC, etc.) though, you can still end up accessing a cached value from the local processor core even if the variable is marked as volatile. The way around this is to implement the proper memory barriers to make sure that any stale values in the actual processor memory caches are flushed and refreshed. These are of course platform-dependent instructions … the good news in your case is the compiler intrinsics you’re using will assure that your don’t face these issues with your AtomicInt class, but I did want to make you aware of this general issue surrounding the volatile keyword.

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

Sidebar

Related Questions

I have a COM Callable Wrapper that I'm using from a VB6 program, but
I have an asp.net usercontrol which represents a popup dialog. Basically, it's a wrapper
Class member functions in Python have to explicitly declare a self parameter which represents
I have C# wrapper code that calls functions from a native (C++) dll. Currently,
I have a wrapper class that delegates its work to a pimpl, and the
I have a list wrapper that maintains two Tstringlists and a TClassList I need
I have a wrapper generic class that intended to be used with a set
I have a main wrapper div that is set 100% width. Inside that i
I have written an O/R database wrapper that generates some wrapper methods for stored
I have a collection of unmanaged dlls with a C# wrapper around them that

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.