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

Ask A Question

Stats

  • Questions 138k
  • Answers 139k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I like Eclipse + PyDev (with extensions). It is available… May 12, 2026 at 7:38 am
  • Editorial Team
    Editorial Team added an answer The return value from SHFileOperation is an int, and should… May 12, 2026 at 7:38 am
  • Editorial Team
    Editorial Team added an answer There is Syn text editor. Somewhat dated but should give… May 12, 2026 at 7:38 am

Related Questions

This question is not so much programming related as it is deployment related. I
I'd like to create a derived control from System.Windows.Forms.ComboBox that is bound to a
I have the below code, which iterates over a list based on a custom
I have two questions.They both are concerning a void in C++,which I am trying

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.