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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:04:39+00:00 2026-06-13T12:04:39+00:00

I have this static class which contains a static variable (a simple int). I’ve

  • 0

I have this static class which contains a static variable (a simple int). I’ve implemented a lock() in the Run() method of the threads, so no other threads can access to this class concurrently, but the variable still goes crazy, displaying duplicates, insanely high values, etc.

This is the class:

public static class ExplorationManager
{
    public static int Counter = 0;

    public static void ExplorerMaker(List<int[]> validPaths, List<string> myParents, string[,] myExplorationMap, List<int[]> myPositions)
    {
        foreach (var thread in validPaths.Select
        (path => new Explorer(myParents, path, myExplorationMap, myPositions)).
        Select(explorer => new Thread(explorer.Explore)))
            {
                thread.Name = "Thread of " + Counter + " generation";
                Counter++; 
                thread.Start();
    }
}

}

Is there a way to make this variable “more” thread-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-06-13T12:04:40+00:00Added an answer on June 13, 2026 at 12:04 pm

    There are at least 2 problems that you need to address in order to increase the safety of this type.

    The first one is to make Counter private. In it’s current form the variable is 100% public and it can be mutated by any piece of code in the application. Today it may be safe but there’s nothing protecting you from making a mistake tomorrow. If you still want other pieces of code to be able to read the property then use an accessor

    private static int m_counter;
    public static int Counter {
      get { return m_counter; }
    }
    

    The second problem is that ++ isn’t a safe operation on a location that is shared amongst threads. It expands out to the following code

    Counter = Counter + 1;
    

    Which is in reality doing

    1. load Counter
    2. load 1
    3. add
    4. store Counter

    A thread can be interrupted an virtually any time. If one thread is interrupted at step 1, 2 or 3 and another thread fully executes the sequence then you will end up adding / storing stale values. This is why ++ is unsafe. The safe way to increment a shared value amongst threads is to use Interlocked.Increment. It’s designed exactly for this purpose

    Interlocked.Increment(ref m_counter);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class called Timestamp. This class contains a static NSDate variable which
This has defeated me. I want to have a static class variable which is
If I have a C++ class which contains a static member variable, does the
I have a non-static class called ImplementHeaderButtons which contains a non-static public method called
I have written an NHibernateSessionFactory class which holds a static Nhibernate ISessionFactory. This is
I have this small class looking like this: private static int field1 = -
I have this user defined function. public partial class UserDefinedFunctions { static int i;
I have a header file which contains a member variable declaration of a static
I have a class which contains a static member, a map of strings to
I have a static class which contains a RoutedUICommand that I would like to

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.