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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:20:40+00:00 2026-06-04T22:20:40+00:00

When I run the following code in debug mode, it’ll successfully finish and exit.

  • 0

When I run the following code in debug mode, it’ll successfully finish and exit. However, if I run the following code in release mode, it’ll get stuck in an infinite loop and never finish.

static void Main(string[] args)
{
    bool stop = false;

    new Thread(() =>
    {
        Thread.Sleep(1000);
        stop = true;
        Console.WriteLine("Set \"stop\" to true.");

    }).Start();

    Console.WriteLine("Entering loop.");

    while (!stop)
    {
    }

    Console.WriteLine("Done.");
}

Which optimization is causing it to get stuck in an infinite loop?

  • 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-06-04T22:20:42+00:00Added an answer on June 4, 2026 at 10:20 pm

    My guess would be processor caching of the stop variable on the main thread. In debug mode the memory model is stricter because the debugger needs to be able to provide a sensible view of the variable’s state across all threads.

    Try making a field and marking it as volatile:

    volatile bool stop = false;
    
    static void Main(string[] args)
    {
    
        new Thread(() =>
        {
            Thread.Sleep(1000);
            stop = true;
            Console.WriteLine("Set \"stop\" to true.");
    
        }).Start();
    
        Console.WriteLine("Entering loop.");
    
        while (!stop)
        {
        }
    
        Console.WriteLine("Done.");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

when I try to run the following code in debug and release mode in
Following StockWatcher tutorial, http://code.google.com/webtoolkit/doc/latest/tutorial/debug.html When I run the app in debug mode, my Safari
When I run code such as the following: - (void)viewDidLoad { #ifdef DEBUG NSLog(@debug);
When I run the following code, I get the exception below: ''# NOTE: ExcelApp
I am running something similar to the following code in debug mode: for (int
when I trying to run following code. var result = from c in db.brand
I run the following code using Adobe Flash Professional CS5.5, Flash Player 11.2.202.235, ActionScript
I tried to run the following code, which is inside of a Bash-script. NUMBER=600
When I run the following code: private void button1_Click(object sender, EventArgs e) { Bitmap
When I run the following code, it only seems to be downloading the first

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.