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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:48:25+00:00 2026-05-11T08:48:25+00:00

In C# 2.0+, is it necessary to lock around a closure that another thread

  • 0

In C# 2.0+, is it necessary to lock around a closure that another thread will execute? Specifically, in the example below, is the locking necessary to ensure that the Maint thread flushes its value of x to shared memory and that thread t reads its value of x from shared memory?

I think it is, but if I’m wrong, please reference e.g. an authoritative (e.g., MSDN) article indicating why not. Thanks!

delegate void Foo(); public static void Main() {   Foo foo = null;   object guard = new object();   int x = 1;   lock (guard)   {     foo = () =>     {       int temp;       lock (guard) temp = x;       Console.WriteLine(temp);     };   }   Thread t = new Thread(() => foo());   t.Start();   t.Join(); } 

Edit: Clarified that I want to know for C# 2.0+, which is to say that the .NET 2.0+’s stronger memory model (than ECMA 335 CLI) is in effect.

  • 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. 2026-05-11T08:48:25+00:00Added an answer on May 11, 2026 at 8:48 am

    Calling any constructor has release semantics in the .NET memory model. (Not in the CLI memory model, but in the .NET one.) So by calling the Thread constructor – and the delegate constructor itself – I believe you’re okay. If you set x to 1 after the final constructor I’d be less sure. (EDIT: It’s possible that the constructor business is more to do with the new Java memory model than .NET, given the later stuff about writes…)

    In fact, I have a suspicion that all writes have release semantics (again, in the implemented .NET memory model) but not all reads have acquire semantics. In other words, the data will always be available to other threads, but it might not be used by those threads. In this case the new thread won’t ‘have’ the old value of the variable – there’s no way it could have logically read the value before it started, so you’re safe on that front.

    I can check up on the ‘all writes’ side of things – I suspect there’s something in Joe Duffy’s blog or book about it.

    EDIT: From ‘Concurrent Programming on Windows’ P516:

    The major difference in the stronger 2.0+ model is that it prevents stores from being reordered.

    I believe that’s enough. I suspect that there are very few people on Stack Overflow who are capable of saying for sure – I wouldn’t fully trust many people who aren’t in the MS CLR or concurrency teams, except maybe Jeff Richter.

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

Sidebar

Related Questions

From a Thread perspective, what is a block, wait and lock? Rather,is it necessary
Is there any locking interface for Python that automatically breaks the lock when the
lock(dictionaryX) { dictionaryX.TryGetValue(key, out value); } is locking necessary while doing lookups to a
Is it necessary to use a lock(lockObj){} block when I fill my ConcurrentDictionary here?
Is that return necessary? Let's say it's in the middle of an action. render(contentType:'text/json',
The run loop for the secondary thread of my application is below. It has
Is it necessary to acquire a lock on a variable before reading it from
I had some code in a shouldOverrideUrlLoading, that downloads the data, converting if necessary,
Is the double check lock pattern necessary when using ReaderWriterLockSlim ? Consider this scenario:
I need a timer equivalent which will periodically execute some specific actions (e.g. updating

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.