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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:48:57+00:00 2026-05-11T03:48:57+00:00

This question is NOT about race-conditions, atomicity, or why you should use locks in

  • 0

This question is NOT about race-conditions, atomicity, or why you should use locks in your code. I already know about those.

UPDATE: My question isn’t ‘does weirdness with volatile memory exist’ (i know it does), my question is ‘doesn’t the .NET runtime abstract that away so you’ll never see it’.

See http://www.yoda.arachsys.com/csharp/threads/volatility.shtml and the first answer on Is a string property itself threadsafe?

(They’re really the same article since one references the other.) One thread sets a bool and the other thread loops forever reading that bool — those articles claim the reading thread might cache the old value and never read the new value, so therefore you need a lock (or use the volatile keyword). They claim the following code will potentially loop forever. Now I agree it’s good practice to lock your variables, but I can’t believe the .NET runtime would really ignore a memory value changing as the article claims. I understand their talk about volatile memory vs non-volatile memory, and I agree they have a valid point in non-managed code, but I can’t believe the .NET runtime won’t correctly abstract that away so that the following code does what you expect. The article even admits the code will ‘almost certainly’ work (though not guaranteed), so I’m calling BS on the claim. Can anyone verify that it’s true the following code won’t always work? Is anyone able to get even one case (maybe you can’t always reproduce it) where this fails?

class BackgroundTaskDemo {     private bool stopping = false;      static void Main()     {         BackgroundTaskDemo demo = new BackgroundTaskDemo();         new Thread(demo.DoWork).Start();         Thread.Sleep(5000);         demo.stopping = true;     }      static void DoWork()     {          while (!stopping)          {                // Do something here          }     } } 
  • 1 1 Answer
  • 3 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. 2026-05-11T03:48:57+00:00Added an answer on May 11, 2026 at 3:48 am

    The point is: it might work, but it isn’t guaranteed to work by the spec. What people are usually after is code that works for the right reasons, rather than working by a fluke combination of the compiler, the runtime and the JIT, which might change between framework versions, the physical CPU, the platform, and things like x86 vs x64.

    Understanding the memory model is a very very complex area, and I don’t claim to be an expert; but people who are real experts in this area assure me that the behaviour you are seeing is not guaranteed.

    You can post as many working examples as you like, but unfortunately that doesn’t prove much other than ‘it usually works’. It certainly doesn’t prove that it is guaranteed to work. It would only take a single counter-example to disprove, but finding it is the problem…

    No, I don’t have one to hand.


    Update with repeatable counter-example:

    using System.Threading; using System; static class BackgroundTaskDemo {     // make this volatile to fix it     private static bool stopping = false;      static void Main()     {         new Thread(DoWork).Start();         Thread.Sleep(5000);         stopping = true;           Console.WriteLine('Main exit');         Console.ReadLine();     }      static void DoWork()     {         int i = 0;         while (!stopping)         {             i++;         }          Console.WriteLine('DoWork exit ' + i);     } } 

    Output:

    Main exit 

    but still running, at full CPU; note that stopping has been set to true by this point. The ReadLine is so that the process doesn’t terminate. The optimization seems to be dependent on the size of the code inside the loop (hence i++). It only works in ‘release’ mode obviously. Add volatile and it all works fine.

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

Sidebar

Ask A Question

Stats

  • Questions 79k
  • Answers 79k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer you need itertools.product: >>> import itertools >>> a = [[1,2,3],[4,5,6],[7,8,9,10]]… May 11, 2026 at 4:09 pm
  • Editorial Team
    Editorial Team added an answer Enter the first letters of your previous command and push… May 11, 2026 at 4:09 pm
  • Editorial Team
    Editorial Team added an answer Yes you can, but you have to apply the same… May 11, 2026 at 4:09 pm

Related Questions

I have a method that contains an asynchronous call like this: public void MyMethod()
As a side project I'm currently writing a server for an age-old game I
This is perhaps similar to previous posts, but I want to be specific about
I was worrying about race conditions in an application I'm designing, when I was

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.