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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:04:03+00:00 2026-05-30T02:04:03+00:00

I have the following scenario in my code (C# .NET 4): I have a

  • 0

I have the following scenario in my code (C# .NET 4):

I have a boolean variable that is initialized to false:

bool b = false;

now I am instantiating number of threads, that each one of them need to read the value of b. the first thread to read b – should change its value to true (and do some logic…), and the others should do nothing. Do I need to use sync mechanism for ‘b’ while reading its value, or can I lock it only while setting its value to true?
This should boost my performance, but I am wondering id its safe..

  • 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-30T02:04:04+00:00Added an answer on May 30, 2026 at 2:04 am

    So the requirements are:

    • multiple threads read a single bool variable
    • if a thread sees a false value it should change it to true and perform additional work
    • if a thread sees a true value then nothing else should happen
    • only one thread should be able to change the value and perform the additional work

    So the code would look like:

    public class Example
    {
      private bool b = false;
    
      public void DoSomething()
      {
        if (!b)
        {
          b = true;
          DoExtraStuff();
        }
      }
    }
    

    This, of course, is not safe if there are multiple threads executing DoSomething and the intention is that DoExtraStuff should only be executed once. The problem is that the threads will race on the read and write of b.

    You really need to make the read and write to b atomic. This can be done using the lock keyword.

    public class Example
    {
      private bool b = false;
      private object locker = new object();
    
      public void DoSomething()
      {
        bool trigger = false;
        lock (locker)
        {
          if (!b)
          {
            b = true;
            trigger = true;
          }
        }
    
        if (trigger)
        {
          DoExtraStuff();
        }
      }
    }
    

    There is an alternate pattern using a CAS operation via Interlocked.CompareExhange. Unfortunately, there is no overload that accepts a bool, but if you are willing to change that bool to an int the following would also work.

    public class Example
    {
      private int b = 0;
    
      public void DoSomething()
      {
        if (Interlocked.CompareExchange(ref b, 0, 1) == 0)
        {
          DoExtraStuff();
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following scenario. The client code only has access to FooHandler, not
I have the following scenario. I have created an ASP.NET web application (framework 3.5)
I have the following scenario: - 64bit Windows Server 2008. - 32bit .NET application
I have the following scenario: From .NET (v3.5, c#) I connect to an OLAP
I have the following code in my asp.net login page: if (Request.QueryString[ReturnUrl] != null)
I have a following scenario that I have to call a function on a
I have the following jQuery code that hides and shows a label when user
I have the following scenario: Entities are loaded from the database. One of them
I have the following scenario where I want to pass in string and a
I have the following scenario, class foo { ... private: char *_test; }; void

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.