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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:43:44+00:00 2026-05-21T18:43:44+00:00

I have a measuring instrument object: public class Instrument { public double Measure() {

  • 0

I have a measuring instrument object:

public class Instrument
{
  public double Measure() 
  { 
    return 0; 
  }
}

I have a device that needs to do some measuring:

public class Device
{
  public Instrument MeasuringInstrument { get; set; }
  public void DoMeasuring()
  {
    var result = this.MeasuringInstrument.Measure();
  }
}

The measuring instrument can only operate on one device at a time, yet many devices may use the same instrument. I’m new to threading, and from what I understand, both of the following solutions have caveats.

public class Instrument
{
  public double Measure() 
  { 
    lock(this)
    {
      return 0; 
    }
  }
}

public class Device
{
  public Instrument MeasuringInstrument { get; set; }
  public void DoMeasuring()
  {
    lock(this.MeasurementInstrument)
    {
      var result = this.MeasuringInstrument.Measure();
    }
  }
}

I’ve read it’s best to lock on private objects, but I don’t know how to do that while still allowing the MeasuringInstrument to be get/set on the Device. Any suggestions?

Thanks much,
Ken

  • 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-21T18:43:45+00:00Added an answer on May 21, 2026 at 6:43 pm

    the usual pattern is to create your own private object just for locking in the case where the obvious choice might be exposed outside of the class, for example:

    public class Instrument
    {
        private object thisLock = new object();
    
        public double Measure() 
        { 
            lock(this.thisLock)
            {
                return 0; 
            }
        }
    }
    
    public class Device
    {
        public Instrument MeasuringInstrument { get; set; }
        private object measuringInstrumentLock = new object();
    
        public void DoMeasuring()
        {
            lock(this.measuringInstrumentLock)
            {
                var result = this.MeasuringInstrument.Measure();
            }
        }
    }
    

    Also, I suspect that you only need one of those two locks (either the one in DoMeasuring or the one in Measure) although that does depend on the missing bits.

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

Sidebar

Related Questions

I have a Device interface: public interface IDevice { double Measure(); } Some classes
I have a class that represents an external physical measuring device. The simplified version
I have found some code on measuring execution time here http://www.dreamincode.net/forums/index.php?showtopic=24685 However, it does
The computer is connected to measuring device via a physical COM1. I have simple
have written this little class, which generates a UUID every time an object of
Due to testing and time measuring I have to write some kind of log
I will have literally tens of millions of instances of some class MyClass and
I have a problem with time measuring that's really bothering me. I am executing
I have some mechanics to measure method execution time. Simply at start of method
I have following macro for measuring time in header file: #define TIMER_START(x) double ___timer__##x

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.