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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:16:14+00:00 2026-05-10T14:16:14+00:00

I have a C# singleton class that multiple classes use. Is access through Instance

  • 0

I have a C# singleton class that multiple classes use. Is access through Instance to the Toggle() method thread-safe? If yes, by what assumptions, rules, etc. If no, why and how can I fix it?

public class MyClass {     private static readonly MyClass instance = new MyClass();      public static MyClass Instance     {         get { return instance; }     }      private int value = 0;      public int Toggle()     {         if(value == 0)          {             value = 1;          }         else if(value == 1)          {              value = 0;          }          return 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. 2026-05-10T14:16:15+00:00Added an answer on May 10, 2026 at 2:16 pm

    Is access through ‘Instance’ to the ‘Toggle()’ class threadsafe? If yes, by what assumptions, rules, etc. If no, why and how can I fix it?

    No, it’s not threadsafe.

    Basically, both threads can run the Toggle function at the same time, so this could happen

        // thread 1 is running this code     if(value == 0)      {         value = 1;          // RIGHT NOW, thread 2 steps in.         // It sees value as 1, so runs the other branch, and changes it to 0         // This causes your method to return 0 even though you actually want 1     }     else if(value == 1)      {          value = 0;      }     return value; 

    You need to operate with the following assumption.

    If 2 threads are running, they can and will interleave and interact with eachother randomly at any point. You can be half way through writing or reading a 64 bit integer or float (on a 32 bit CPU) and another thread can jump in and change it out from underneath you.

    If the 2 threads never access anything in common, it doesn’t matter, but as soon as they do, you need to prevent them from stepping on each others toes. The way to do this in .NET is with locks.

    You can decide what and where to lock by thinking about things like this:

    For a given block of code, if the value of something got changed out from underneath me, would it matter? If it would, you need to lock that something for the duration of the code where it would matter.

    Looking at your example again

        // we read value here     if(value == 0)      {         value = 1;      }     else if(value == 1)      {          value = 0;      }     // and we return it here     return value; 

    In order for this to return what we expect it to, we assume that value won’t get changed between the read and the return. In order for this assumption to actually be correct, you need to lock value for the duration of that code block.

    So you’d do this:

    lock( value ) {      if(value == 0)       ... // all your code here      return value; } 

    HOWEVER

    In .NET you can only lock Reference Types. Int32 is a Value Type, so we can’t lock it.
    We solve this by introducing a ‘dummy’ object, and locking that wherever we’d want to lock ‘value’.

    This is what Ben Scheirman is referring to.

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

Sidebar

Ask A Question

Stats

  • Questions 109k
  • Answers 109k
  • 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 Fur such cases there is a ScriptManagerProxy class that you… May 11, 2026 at 9:24 pm
  • Editorial Team
    Editorial Team added an answer JSR-94 is the only standard in wide use but, sadly,… May 11, 2026 at 9:24 pm
  • Editorial Team
    Editorial Team added an answer multipart/form-data is a lot bulkier than application/x-www-form-urlencoded; the latter is… May 11, 2026 at 9:24 pm

Related Questions

I have a singleton class that inherits from sprite so that it can access
I have created a webservice in .net 2.0, C#. I need to log some
I'm using C#. I have a products class with fields like sku, name, description....
I have a set of functions I want to be available to my web

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.