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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:20:43+00:00 2026-05-13T07:20:43+00:00

I have the feeling that I should not care about thread safe accessing /

  • 0

I have the feeling that I should not care about thread safe accessing / writing to an

public static int MyVar = 12;

in ASP .NET.

I read/write to this variable from various user threads. Let’s suppose this variable will store the numbers of clicks on a certain button/link.

My theory is that no thread can read/write to this variable at the same time. It’s just a simple variable of 4 bytes.

I do care about thread safe, but only for refference objects and List instances or other types that take more cycles to read/update.

I am wrong with my presumption ?

EDIT

I understand this depend of my scenario, but wasn’t that the point of the question. The question is: it is right that can be written thread safe code with an (static int) variable without using lock keyword ?

It is my problem to write correct code. The answer seems to be: Yes, if you write correct and simple code, and not to much complicated, you can create thread safe functions without the need of lock keyword.

  • 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-13T07:20:43+00:00Added an answer on May 13, 2026 at 7:20 am

    If one thread simply sets the value and another thread reads the value, then a lock is not necessary; the read and write are atomic. But if multiple threads might be updating it and are also reading it to do the update (e.g., increment), then you definitely do need some kind of synchronization. If only one thread is ever going to update it even for an increment, then I would argue that no synchronization is necessary.

    Edit (three years later) It might also be desirable to add the volatile keyword to the declaration to ensure that reads of the value always get the latest value (assuming that matters in the application).

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

Sidebar

Related Questions

I have the feeling that is easy to find samples, tutorials and simple examples
I have a feeling that I already know the answer to this one, but
I have a feeling that there must be client-server synchronization patterns out there. But
I have the feeling that Flash -based ( or Silverlight -based) websites are generally
I have a feeling this is a repost but I can't seem to find
I curious to how different people solve integration of systems. I have a feeling
I'm the leader of a small web development team, and I have a feeling
Ok I have some code that boils down to a pattern like this class
I have: A passive STS login application that is also an identity provider. An
Introduction In my current organisation, we have many desktop and web applications all feeding

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.