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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:24:10+00:00 2026-05-11T03:24:10+00:00

I just came across the question – is the access of an object safely

  • 0

I just came across the question – is the access of an object safely possible across threads in C#.

E.g. with the code

//Somewhere long ago MyClass myVar = new MyClass('First Instance');  //Then later, happening at the same time //In thread one myVar = new MyClass('Second Instance');  //In thread two myVar.PrintName(); 

I don’t care whether the first or the second instance is used, but is it possible that myVar is at some point not valid at all (e.g. pointing to a non existing location, because maybe the object reference is only partly updated before used in the other thread)


Side question about locks:

If I leave a lock – are then all outstanding writes committed to memory?

My question here is if I reference a variable in a lock, I know only one thread can access the lock at the same time – but can it happen that I write to the variable in one thread (and the write is only done to the cache), and after that in the other thread even inside the lock getting the old value of the variable (because the cache is not committed or I still have an old value in the cache of this thread)?

  • 1 1 Answer
  • 2 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. 2026-05-11T03:24:10+00:00Added an answer on May 11, 2026 at 3:24 am

    To your first. Its almost certainly safe but I would lock it none the less.

    As to your cache question what cache are you talking about, the only ones I can think of are the CPU caches, if we had worry about whether these we’re dirty and needed flushing we’d all be in a lot of trouble.

    Edit: From your comment I see that the CPU cache is what you are refering to.

    Mutli-core and Multi-processor hardware ensures that any data in a cache that is out-of-date will not be used.

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

Sidebar

Related Questions

I just came across a piece of code written by my ex-colleague few years
I just came across this question in the Go FAQ, and it reminded me
I was just reading some code and I came across a function named __toString
Quick question. I just came across the following in a JS file: if (0)
I just came across some code that overrides ToString() and returns some critical information
I was just browsing and came across this question: Action vs delegate event The
Please correct me if this question is duplicated. I just came across Unity3D and
I just came across this question about initializing local variables. Many of the answers
I just came across an exam question whilst preparing for an exam and I'm
Possible Duplicate: What do two question marks together mean in C#? I just came

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.