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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:55:42+00:00 2026-05-23T12:55:42+00:00

This is (roughly) what I have: class A { public bool IsInUpdate = false;

  • 0

This is (roughly) what I have:

class A
{
    public bool IsInUpdate = false;
    public void Update()
    {
        IsInUpdate = true;

        //(...do stuff...)

        IsInUpdate = false;
    }
}

class B
{
    A a_inst;
    System.Threading.Thread physicsThread = null;

        void Draw()
        {
            physicsThread = new System.Threading.Thread(a_inst.Update);
            physicsThread.Start();
        }


    void Update()
    {
        while(physicsThread.IsAlive)
        {
            // Right here there can be cases where physicsThread.IsAlive is true but IsInUpdate is false, how does that happen?
        }

        (...do stuff...)
    }


}

Question is in the comments of the code. Basically the physics thread instance says it’s alive but the function it’s calling has clearly been finished calling (as can be seen by the bool being set to false).

Any ideas why this happens? All I want to do is make sure the update function in class B does not execute until the threaded update function of class A has executed…

  • 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-23T12:55:43+00:00Added an answer on May 23, 2026 at 12:55 pm

    Since IsInUpdate is simply a public field (and non-volatile at that), there are no guarantees about what you see; the normal sensible rules about what you see only apply on a single thread, and you have not guarded any of this data. There is also an edge-case around the start condition, but personally I would be using either lock (if you need to wait for it to complete), or maybe Interlocked if you just need to know if it is active.

    For example:

    class A
    {
        private readonly object syncLock = new object();
        public object SyncLock { get { return syncLock; } }
        public void Update()
        {
            lock(SyncLock)
            {
    
                //(...do stuff...)
    
            }
        }
    }
    

    and

    void Update()
    {
        lock(a_inst.SyncLock)
        {
            (...do stuff...)
        }
    }
    

    With the above, you are guaranteed that only one thread will have the lock at any time, so if you get to “do stuff” you know that it isn’t also running the other Update(). If you need to wait etc there are also Wait() / Pulse() methods against locks, or you can use gates such as ManualResetEvent/AutoResetEvent.

    Things like lock also ensure correct memory barriers between the threads, so you see the correct data.

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

Sidebar

Related Questions

I have a class that roughly looks like this: public class ViewModel { public
I have just refactored a colleague's code that, roughly, looked like this... public class
Let's say I have objects which look very roughly like this: class object {
I have a class that looks roughly like this: template<std::size_t dim> class Foo {
I have a an XSD that looks like this (roughly) <xs:schema id=Appointment targetNamespace=http://tempuri.org/Record.xsd elementFormDefault=qualified
I have roughly the following code. Could this be made nicer or more efficient?
I have roughly 12 computers that each have the same script on them. This
I have html code that looks roughly like this: <div id=id1> <div id=id2> <p>some
I have a structure that roughly looks like this: List<ProductLine> -> ID Name ...
A GWT tree looks roughly like this: <div class=gwt-Tree> <div style=padding-top: 3px; padding-right: 3px;

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.