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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:21:12+00:00 2026-05-30T14:21:12+00:00

Taking CPU caching and any possible compiler optimizations into account: Is it possible for

  • 0

Taking CPU caching and any possible compiler optimizations into account:

  1. Is it possible for “False” to be written to the console?
  2. Is option 1 setting up a memory fence that guarantees the freshness of the object reference and the field?
  3. Is option 2 less “safe” than option 1 in any way?

    class Program
    {
        class Test
        {
            internal bool value = false;
        }
    
        static void Main(string[] args)
        {
            Test test = new Test();
    
            //Option 1
            ThreadPool.QueueUserWorkItem((s) => 
            {
                Thread.Sleep(1000);
                Console.WriteLine(((Test)s).value); 
            }, test);
    
            //Option 2
            ThreadPool.QueueUserWorkItem((s) =>
            {
                Thread.Sleep(1000);
                Console.WriteLine(test.value);
            });
    
            test.value = true;
            Console.ReadLine();
        }
    }
    
  • 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-30T14:21:13+00:00Added an answer on May 30, 2026 at 2:21 pm
    1. Yes, of course. There is no synchronization to prevent either option #1 or option #2’s worker threads from printing out the false value before the main thread sets it to true. While the contrived example uses a Thread.Sleep to prevent it from ever realistically occurring, there is no real synchronization here.

    2. No, option 1 is simply sending a reference to s to the thread pool for forwarding to the delegate. Any memory fencing that happens should be considered an implementation detail, and as far as I am aware none occurs. There is certainly nothing (other than the aforementioned Thread.Sleep()) preventing option #1’s worker thread from printing “false” before the main thread sets it to true.

    3. Option 2 is less safe in that you are closing over a variable which may be modified in a less contrived example. See Eric Lippert’s blog post on the subject of closing over loop variables– the same advice applies to option #2.

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

Sidebar

Related Questions

Taking CPU affinity into account, will such an environment be useful with threading? Or
Well, this is my first foray into memory profiling a .NET app (CPU tuning
What I'm talking about is: Is it possible that under certain circumstances the CPU
I have a user complaining that my app is taking a lot of CPU
I'm writing a shell script to find out if any process is taking too
Whats a good server side language for doing some pretty cpu and memory intensive
We were able to solve a high CPU usage problem by taking advantage of
I have a 3rd DLL that is built for Any CPU. It needs (for
I am having issues listing the memory and CPU usage of processes in C#.
Possible Duplicate: What Android tools and methods work best to find memory/resource leaks? I

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.