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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:28:01+00:00 2026-06-17T09:28:01+00:00

I was looking at the Microsoft MSDN reference page regarding the modifier volatile, and

  • 0

I was looking at the Microsoft MSDN reference page regarding the modifier “volatile”, and was a little unsure as to the way in which the snippet they provided, waited for a thread to complete execution.

I know it is only example code, and that the thread completed very quickly, but I believe that the code below is not very good for developers trying to understand threading.

I believe Microsoft have actually presented a code snippet which will introduce a “tight-loop” in the code. Now I appreciate that it will not affect this code snippet (that much), but if a developer takes this code and tries to use it for some multi-threaded code that is a little more intensive, I would presume the “tight-loop” issue would arise?

using System;
using System.Threading;
class Test
{
   public static int result;   
   public static volatile bool finished;
   static void Thread2() {
      result = 143;    
      finished = true; 
   }
   static void Main() {
      finished = false;
      // Run Thread2() in a new thread
      new Thread(new ThreadStart(Thread2)).Start();
      // Wait for Thread2 to signal that it has a result by setting
      // finished to true.
      for (;;) {
         if (finished) {
            Console.WriteLine("result = {0}", result);
            return;
         }
      }
   }
}

Snippet reference:
http://msdn.microsoft.com/en-gb/library/aa645755(v=vs.71).aspx

What would be the better way to wait for the thread to finish, in the example above, which would not introduce this “tight-loop” situation?

Or, will a “tight-loop” not actually be introduced at all?

Please note, that the purpose of the snippet is to demonstrate the “volatile” keyword, so using a Thread.Join() would take the snippet out of context I believe.

  • 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-17T09:28:02+00:00Added an answer on June 17, 2026 at 9:28 am

    This snippet doesn’t illustrate how to wait, this just illustrates access to a volatile field from different threads.

    To wait for your background thread simply, you can use this snippet:

    AutoResetEvent autoEvent = new AutoResetEvent(false);
    ThreadPool.QueueUserWorkItem((o) =>
                                  {
                                       // do your stuff
                                       ((AutoResetEvent)o).Set();
                                  }, autoEvent);
    autoEvent.WaitOne();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking at http://msdn.microsoft.com/en-us/library/aa691278(VS.71).aspx , which said read and write of int is
I'm looking at the developers reference for IE10 ( http://msdn.microsoft.com/en-us/library/ie/hh673549(v=vs.85).aspx ) and I'm basically
I'm looking at the following reference for making asynchronous web requests with C#: http://msdn.microsoft.com/en-us/library/86wf6409%28v=vs.100%29.aspx
I'm looking at the MSDN subscription comparison ( http://msdn.microsoft.com/en-us/subscriptions/subscriptionschart.aspx ) and am a bit
I've been looking into the Timer class ( http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx ), but the thing about
I'm looking at the RegisterHotKey Function: http://msdn.microsoft.com/en-us/library/ms646309(VS.85).aspx BOOL RegisterHotKey( __in HWND hWnd, __in int
I'm looking at WCF documentation in the MSDN ( http://msdn.microsoft.com/en-us/library/bb332338.aspx ), and have come
I'm looking at the TPL exception handling example from MSDN @ http://msdn.microsoft.com/en-us/library/dd537614(v=VS.100).aspx The basic
MSDN has the functionality I'm looking for here . Since microsoft recognizes the usefulness
I was looking at the example in Microsoft KB318804 but they use the threadId

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.