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

  • Home
  • SEARCH
  • 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 7728083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:37:41+00:00 2026-06-01T05:37:41+00:00

Given doubles can’t be used in volatile fields, is this a safe alternative? Or

  • 0

Given doubles can’t be used in volatile fields, is this a safe alternative? Or could it lead to a null pointer exception.

public sealed class DoubleStorage
{
    public readonly double Value;

    public DoubleStorage(double value)
    {
        this.Value = value;
    }

    public static readonly DoubleStorage Zero = new DoubleStorage(0);
}

public sealed class Val
{
    private volatile DoubleStorage boostResult = DoubleStorage.Zero;

    public double ThreadSafeDouble
    {
        set
        {
            //is this assignment attomic?
            boostResult = new DoubleStorage(value);
        }
        get
        {
            //or could this give null pointer exception?
            return boostResult.Value;
        }
    }
}
  • 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-01T05:37:42+00:00Added an answer on June 1, 2026 at 5:37 am

    Yes, accessing references is atomic.

    However, you can just box the double, you don’t have to create a class to encapsulate it:

    public sealed class Val {
    
      private volatile object boostResult = 0.0;
    
      public double ThreadSafeDouble {
        set {
            boostResult = value;
        }
        get {
            return (double)boostResult;
        }
      }
    }
    

    Assigning a double to the variable will create an object that boxes the double, then assigns the reference to the variable. As the reference assignment is atomic, it’s thread safe.

    When unboxing the double, reading the reference from the variable is atomic, so it’s thread safe eventhough reading the double isn’t atomic. As the boxed double is immutable, a specific boxed value will never change.


    Or what about a generic version:

    public sealed class ThreadSafe<T> where T : struct {
    
      private volatile object _value;
    
      public ThreadSafe() {
        Value = default(T);
      }
    
      public ThreadSafe(T value) {
        Value = value;
      }
    
      public T Value {
        get {
          return (T)_value;
        }
        set {
          _value = value;
        }
      }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can you help me out with this question please. Question: Given the following array
Given two arrays of doubles, how Can I compute the F Test? (F test
Given this markup: // Calendar.html?date=1/2/2003 <script> $(function() { $('.inlinedatepicker').datepicker(); }); </script> ... <div class=inlinedatepicker
Given a package, how can I automatically find all its sub-packages?
How can I serialize doubles and floats in C? I have the following code
I'm sure I've seen this described before but can't for the life of me
Given the string name of a class in PHP, how can I access one
How can I convert a double-precision hexadecimal back into a double? For example, given
I want to convert a given time into epoch(time_t) and vice versa. can anyone
Have noticed the following behaviour when trying to compare two doubles. Given the following

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.