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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:39:03+00:00 2026-06-07T02:39:03+00:00

Note I tend to write lock-free code so I try to avoid any types

  • 0

Note I tend to write lock-free code so I try to avoid any types of lock where possible. Instead I just use while(true) loop because I have a lot of CPU power.

According to that http://msdn.microsoft.com/en-us/library/aa691278%28VS.71%29.aspx double variable update is not atomic.

I’m concerned about two issues:

  • if one thread modify variable of field or property and another thread read it at the same time i want to have either previous or new value, but I don’t want to receive something strange. I.e. if one thread changes value from 5.5 to 15.15 I want to have in another thread one of these two numbers, but not 5.15 or 15.5 or anything else.
  • if one thread already updated value and another thread read it after that I want to receive latest, up to date, value. I was thinking that volatile keyword can help with that but in seems it can’t, because “Volatile does not guarantee freshness of a value. It prevents some optimizations, but does not guarantee thread synchronization.” as said here are c# primitive arrays volatile?

Questions:

  • Am I correct that without synchronization both of these issues may appear?
  • If you can give me short example which proves that without synchronization it will not work – that would be nice
  • How should I access double field or variable or property to have always real up to date value? Will “synchronization” guarantee “freshness”? What would be the fastest way for that? spinlock or something?

Currently I use a lot of double and decimal variables/field/properties in my program and almost everythig works fine, so I really confused because I ofthen access them from different threads without any synchronization and that just works… But now I’m thinking that probably it would be better to use float to have “built-in syncrhonization”

  • 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-07T02:39:05+00:00Added an answer on June 7, 2026 at 2:39 am

    Yes, you need to do something. double and decimal are not guaranteed to be atomic, so if you don’t protect it you could get a torn value – i.e. your first bullet is entirely correct.

    Re volatile; it is moot; you are not allowed to have a volatile field that is double or decimal, so the simplest answer is: lock.

    Getting double to fail is a royal PITA; but here’s a torn-value example featuring decimal (note the numbers of success/fail will change each iteration, even though the data is the same; this is the randomness of thread scheduling):

    using System;
    using System.Threading;
    static class Program
    {
        private static decimal shared ;
        static void Main()
        {
            Random random = new Random(12345);
            decimal[] values = new decimal[20];
            Console.WriteLine("Values:");
            for (int i = 0; i < values.Length; i++)
            {
                values[i] = (decimal)random.NextDouble();
                Console.WriteLine(values[i]);
            }
            Console.WriteLine();
            object allAtOnce = new object();
            int waiting = 10;
            shared = values[0];
            int correct = 0, fail = 0;
            for(int i = 0 ; i < 10 ; i++)
            {
                Thread thread = new Thread(() =>
                {
                    lock(allAtOnce)
                    {
                        if (Interlocked.Decrement(ref waiting) == 0)
                        {
                            Monitor.PulseAll(allAtOnce);
                        } else
                        {
                            Monitor.Wait(allAtOnce);
                        }
                    }
                    for(int j = 0 ; j < 1000 ; j++)
                    {
                        for(int k = 0 ; k < values.Length ; k++)
                        {
                            Thread.MemoryBarrier();
                            var tmp = shared;
                            if(Array.IndexOf(values, tmp) < 0)
                            {
                                Console.WriteLine("Invalid value detected: " + tmp);
                                Interlocked.Increment(ref fail);
                            } else
                            {
                                Interlocked.Increment(ref correct);
                            }
                            shared = values[k];
                        }
                    }
                    if (Interlocked.Increment(ref waiting) == 10)
                    {
                        Console.WriteLine("{0} correct, {1} fails",
                            Interlocked.CompareExchange(ref correct, 0, 0),
                            Interlocked.CompareExchange(ref fail, 0, 0));
                        Console.WriteLine("All done; press any key");
                        Console.ReadKey();
                    }
                });
                thread.IsBackground = false;
                thread.Start();
            }
        }
    }
    

    The key point; the language makes no guarantees for the atomicity of double. In reality, I expect you’ll be fine, but most subtle problems caused by threading are due to using “I expect” instead of “I can guarantee”.

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

Sidebar

Related Questions

Note: I am not asking how to use Google Code's SVN repo as a
(Note: This is not a question about what is the best way with code
EDIT: Note that due to the way hard drives actually write data, none of
I am trying to write some code that works on both Linux and Win32.
I try to make my code fool-proof, but I've noticed that it takes a
When I check-in code, I sometime write very long, detailed checkin notes, other times
Having worked with Django , I've seen that people tend to reccommend the use
I'm attempting to place some embed code into a Premium WordPress Theme. NOTE: I'm
NOTE: I use pseudocode in my question lets say i have a class called
Note, this is not a duplicate of .prop() vs .attr() ; that question refers

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.