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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:59:58+00:00 2026-06-11T04:59:58+00:00

I have a AVL tree data structure, where every node has its own lock.

  • 0

I have a AVL tree data structure, where every node has its own lock. Its because there is more writers which are trying to acces a node.

class Node
{
    public ReaderWriterLockSlim ww;
    // ...
    public Node()
    {
        ww = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
        // ...
    }
}
class AVL_tree
{
    public Node root;
    // ...
    public void Write(int value)
    {
        root = new Node();
        root.ww.EnterWriteLock();
        if (!root.ww.IsWriteLockHeld) throw new Exception("Why?");
        // ...
        root.ww.ExitWriteLock();
    }
}

Each writer is starting in new thread

class Program{
    public static AVL_Tree data;
    static void Main()
    {
        data = new AVL_Tree();
        List<Thread> vlakna = new List<Thread>();
        for (int i = 1; i < 10; i++)
            vlakna.Add(new Thread(Write));
        foreach (Thread vlakno in vlakna)
            vlakno.Start();
    }
    public static void Write() // Write some random data into the tree
    {
        Random rnd = new Random(DateTime.Now.Millisecond);
        data.Writer(rnd.Next(1, 999));
    }

Writer doesn’t look exactly like this, there is more nodes and more of code, but the problem is following:

After locking the node, the lock is not held, sometimes. I don’t understand why.
Have anyone some kind of explanation.

*Sometimes means that I can’t find out when it is going to happend.

  • 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-11T05:00:00+00:00Added an answer on June 11, 2026 at 5:00 am

    Are you sure it is being accessed by a different thread? If it is the same thread recursively acquiring the lock, it will be granted to every recursion level.

    To see if this is the case, change the lock recursion policy to NoRecursion and see if you get an exception.

    [EDIT]

    Here’s another thought: You have a race condition.

    Each thread you start is calling data.Write(), i.e. AVL_tree.Write().

    Inside AVL_tree.Write() you assign a new root node.

    Let’s examine your AVL_tree.Write():

    class AVL_tree
    {
        public Node root;
        // ...
        public void Write(int value)
        {
            root = new Node();         // [A]
            root.ww.EnterWriteLock();  // [B]
            if (!root.ww.IsWriteLockHeld) throw new Exception("Why?"); // [C]
            // ...
            root.ww.ExitWriteLock();
        }
    }
    

    Imagine that thread 1 has executed up to line [B], and is ABOUT TO execute line [C].

    Now imagine that thread 2 comes along and executes line [A] and is ABOUT TO execute line [B].

    At this point, the root field has been overwritten by a new one, one that has NOT yet acquired its write lock.

    Now imagine that thread 1 continues to line [C]. It looks at the root (now the one that was newed by thread 2), and discovers that the write lock is not held and therefore throws an exception.

    That is what I think is happening.

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

Sidebar

Related Questions

I'm implementing an AVL binary tree data structure in C# .NET 2.0 (possibly moving
Have data that has this kind of structure. Will be in ascending order by
Have data that has this kind of structure: $input = [ { animal: 'cat',
For homework I was assigned to make an AVL Tree data structure. I am
I have a data structures homework, that in addition to the regular AVL tree
I have an AVL tree while each node consists of: Key Value The AVL
I have an AVL Tree. Each node looks like this: typedef struct { Node
I'm working on an AVL tree assignment and I have a quick question about
I have a large AVL Tree which I build some times during program from
I have in my homework some question about data structure: I have elements which

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.