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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:41:51+00:00 2026-05-28T00:41:51+00:00

Simplified illustration below, how does .NET deal with such a situation? and if it

  • 0
  • Simplified illustration below, how does .NET deal with such a situation?
  • and if it would cause problems, would i have to lock/gate access to each and every field/property that might at times be written to + accessed from different threads?

A field somewhere

public class CrossRoads(){
    public int _timeouts;
}

A background thread writer

public void TimeIsUp(CrossRoads crossRoads){
    crossRoads._timeouts++;
}

Possibly at the same time, trying to read elsewhere

public void HowManyTimeOuts(CrossRoads crossRoads){
    int timeOuts = crossRoads._timeouts;
}
  • 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-28T00:41:51+00:00Added an answer on May 28, 2026 at 12:41 am

    The simple answer is that the above code has the ability to cause problems if accessed simultaneously from multiple threads.

    The .Net framework provides two solutions: interlocking and thread synchronization.

    For simple data type manipulation (i.e. ints), interlocking using the Interlocked class will work correctly and is the recommended approach.

    In fact, interlocked provides specific methods (Increment and Decrement) that make this process easy:

    Add an IncrementCount method to your CrossRoads class:

    public void IncrementCount() {
        Interlocked.Increment(ref _timeouts);
    }
    

    Then call this from your background worker:

    public void TimeIsUp(CrossRoads crossRoads){
        crossRoads.IncrementCount();
    }
    

    The reading of the value, unless of a 64-bit value on a 32-bit OS, are atomic. See the Interlocked.Read method documentation for more detail.

    For class objects or more complex operations, you will need to use thread synchronization locking (lock in C# or SyncLock in VB.Net).

    This is accomplished by creating a static synchronization object at the level the lock is to be applied (for example, inside your class), obtaining a lock on that object, and performing (only) the necessary operations inside that lock:

        private static object SynchronizationObject = new Object();
    
        public void PerformSomeCriticalWork()
        {
            lock (SynchronizationObject)
            {
                // do some critical work
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a menu with the following structure (simplified for illustration purposes): Menu Bar
Simplified for example, I have two tables, groups and items . items ( id,
Here is a simplified version of my database model. I have two tables: Image,
Below is my (simplified) schema (in MySQL ver. 5.0.51b) and my strategy for updating
I have a fairly complex WordPress + bbPress installation and I would like to
simplified, i have three elements A, B and C. B and C are together
Simplified, I have a build release (BR) project in Jenkins that checks out and
Simplified, I have to solve the following problem: You have a 2-dimensional array filled
I have models (simplified example): class Group(models.Model): name = models.CharField(max_length = 32) class Person(models.Model):
Simplified version of the problem: So I have this query which is inside a

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.