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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T00:05:35+00:00 2026-05-21T00:05:35+00:00

I have a class which uses a read-write lock. I want to see if

  • 0

I have a class which uses a read-write lock.

I want to see if I’ve locked and protected all the methods correctly.
Is there a design pattern for a test to check if the locks are set correctly ?

Edit:
Some clarifications:

It’s a C# code derived from a C++/CLI Code which has locks in the C++ level… Not that simple. That’s why I’m looking for a design for a test, not for a design for how to lock it.

There are a few things that need to be checks while multithreading:

No Deadlocks (Most obvious)
Correctness (If I update it in 1 thread it will be seen in the other)
Atomic write (If I write in one thread, I will able to read only when the full value is written)
Fairness (Might be more theoretical to prove, should be true if i use Mutex anyway)

  • 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-21T00:05:35+00:00Added an answer on May 21, 2026 at 12:05 am

    You might want to read up on SyncLock and SyncRoot which are used to create a common pattern on how to lock your objects. Since you do not want to lock an enitre object you often have a SyncRoot that you lock on.

    An example for this is ArrayList or ICollection which both have a SyncRoot that you should use to lock the collection. You can read more about Thread Synchronization on MSDN.

    But generally it’s just like Marc pointed out, be carefull, test, test, test and do some more test!

    Example of SyncLock

    public class Person
    {
        public decimal Salary { get;set; }
        public string Name { get; set; }
        public readonly object SyncRoot = new object();
    }
    

    You can then approach a locking like this:

    var person = new Person { Name = "Bill", Salary = 1000000 };
    
    lock(person.SyncRoot)
    {
        IncreasSalary();
    }
    

    A bad pattern is to do lock(this) NEVER do that!

    There is also somthing called Double-checked locking which is not specific to .NET you might also want to read this paper on “Strategized Locking, Thread-safe Interface, and Scoped Locking”.

    Testing for thread safety

    If you want to test for thread safety I recommend you check out “Unit test for thread safety”, the accepted answer points to Microsoft Chess which can help you identify deadlocks in your application.

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

Sidebar

Related Questions

I have the following class which uses BinaryReader internally and implements IDisposable. class DisposableClass
I have a container class which uses boost::optional to hold the value. Here is
I have an application App1 which defines class A and uses instances of this
I have some logic, which defines and uses some user-defined types, like these: class
I have the following code which uses an array to write the result to
I have a class which uses an ISet as a collection type as below:
I have a service class in my Android app which uses a timer. The
I have a class which I'm serialising to send over a unix socket and
I have a class which implements UserControl. In .NET 2005, a Dispose method is
I have a class which is marked with a custom attribute, like this: public

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.