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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:59:35+00:00 2026-05-18T19:59:35+00:00

This question keeps haunting me: in a multithreaded/multiprocessor environment – is it necessary to

  • 0

This question keeps haunting me: in a multithreaded/multiprocessor environment – is it necessary to use explicit locks to synchronize access to shared variables?

Here is a scenario: I have a global variable pointing to a shared object. When an instance of the object is created, the reference to it is placed in the variable and becomes accessible to other threads/processors.

The object itself is immutable – once created it never changes, because of this multiple threads can access it without any additional synchronization.

Once in a while I need to update the object. I do it be creating a new instance of the object on the side and then place a reference to the new object in the global variable.

So here is the question: Can I consider replacing the reference to be an atomic operation. In other words does the variable always have a valid reference to my object – either the old one or the new one?

  • 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-18T19:59:35+00:00Added an answer on May 18, 2026 at 7:59 pm

    is it necessary to use explicit locks to synchronize access to shared variables?

    Necessary? No. A good idea? Yes. Low-lock techniques are very hard to get right and rarely justified. Remember, a lock is slow when contested; if your locks are being contested, fix the problem that is causing them to be contested, rather than going to a low-lock solution.

    The object itself is immutable – once created it never changes, because of this multiple threads can access it without any additional synchronization.

    Awesome.

    Once in a while I need to update the object. I do it be creating a new instance of the object on the side and then place a reference to the new object in the global variable.

    So here is the question: Can I consider replacing the reference to be an atomic operation. In other words does the variable always have a valid reference to my object – either the old one or the new one?

    Yes. The C# specification guarantees that operations on references are atomic. However atomicity is just one small part of thread safety. Atomicity merely guarantees that every time you look at the variable, you get a valid reference out of it. It does not guarantee that every time you look at the variable you get the current reference out of it. Nor does it guarantee that any two threads see the same sequence of changes in the same order. Nor does it guarantee that two atomic updates to two different variables are seen in the order they happened on every thread. Atomicity guarantees you practically nothing, and it might not guarantee you enough to make your program work the way you think it should.

    My advice: if you can avoid accessing this variable on multiple threads, do so. If you cannot avoid it, put locks around it. Only if you find that for performance reasons the locks are too slow and you cannot eliminate enough contention should you consider going to dangerous low-lock techniques like making the variable volatile, using interlocked exchanges, and so on.

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

Sidebar

Related Questions

This question keeps haunting me: in a multithreaded/multiprocessor environment - is it necessary to
This question is related to this question: Node.js running under IIS Express Keeps Crashing
EDIT I added this note to explain why I keep this question here. I
This is my first question on stackoverflow so I'll try to keep it concise
So this is more of a generic question. I seem to keep finding myself
This question is directly related to this SO question I posed about 15 minutes
This question is kind of a follow up to this question I asked a
This question is really basic. What is the performance difference between removing a UIView
This question is similar in concept to this one , except I see I
This question is about good programming practices and avoiding potential holes. I read Joshua

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.