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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:17:34+00:00 2026-05-24T19:17:34+00:00

I have a method similar to: public static void DoSomething (string param1, string param2,

  • 0

I have a method similar to:

public static void DoSomething (string param1, string param2, SomeObject o) 
{
   //.....

   lock(o) 
   {
       o.Things.Add(param1);
       o.Update();
       // etc....
   }
}

A few points:

  1. Is locking in this way bad practice?
  2. Should I lock on a private static object instead?
  3. If so, why?
  • 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-24T19:17:35+00:00Added an answer on May 24, 2026 at 7:17 pm

    To minimize side effects, the object being locked on should not be the object being manipulated but rather a separate object designated for locking.

    Depending on your requirements, there are a few options for handling this issue:

    Variant A: Private locking object

    Choose this if you just want to ensure that DoSomething does not conflict with a parallel instance of DoSomething.

    private static readonly object doSomethingLock = new object();
    
    public static void DoSomething (string param1, string param2, SomeObject o) 
    {
       //.....
    
       lock(doSomethingLock) 
       {
           o.Things.Add(param1);
           o.Update();
           // etc....
       }
    }
    

    Variant B: Pass locking object as a parameter

    Choose this if access to o must be thread-safe even outside of DoSomething, i.e., if the possibility exists that someone else writes a method DoSomethingElse which runs in parallel to DoSomething and which must not interfere with the lock block in DoSomething:

    public static void DoSomething (string param1, string param2, SomeObject o, object someObjectLock) 
    {
       //.....
    
       lock(someObjectLock) 
       {
           o.Things.Add(param1);
           o.Update();
           // etc....
       }
    }
    

    Variant C: Create SyncRoot property

    If you have control over the implementation of SomeObject, it might be convenient to provide the locking object as a property. That way, you can implement Variant B without having to pass around a second parameter:

    class SomeObject
    {
        private readonly object syncRoot = new object();
    
        public object SyncRoot { get { return syncRoot; } }
    
        ...
    }
    

    Then, you just use lock(o.SyncRoot) in DoSomething. That’s the pattern some of the BCL classes use, e.g., Array.SyncRoot, ICollection.SyncRoot.

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

Sidebar

Related Questions

I have something similar to the following method: public ActionResult Details(int id) { var
I have an Obj-C method similar to this: -(void)getUserDefaults:(BOOL *)refreshDefaults { PostAppDelegate *appDelegate =
I have a class in C# with a template and static method similar to
All the generated webservice-stubs from our backend have an equals-method similar to this one:
I have a SafeInvoke Control extension method similar to the one Greg D discusses
I have a method which takes params object[] such as: void Foo(params object[] items)
I have a typed message broker similar to what Caliburn provides: public interface IMessageBroker
Update: Here's a similar question Suppose I have a DataTable with a few thousand
We have a class hierarchy similar to this one: public class TestDereference { private
I have a similar problem to the post Accessing a static property of a

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.