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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:46:50+00:00 2026-05-11T02:46:50+00:00

The following C# class is used in a multithreaded enviroment. I removed very much

  • 0

The following C# class is used in a multithreaded enviroment. I removed very much of the actual code. The problem occurs when calling MethodA and MethodB almost simultaneously. The order of the lock in the IsDepleted property doesn’t solves the problem. Removing lock(WaitingQueue) from the IsDepleted property solves the deadlock, but this solution causes a problem when another thread adds/removes an item from the WaitingQueue between the WaitingQueue.Count == 0 and Processing.Count == 0 statements.

using System.Collections.Generic;  class Example {     bool IsDepleted     {         get         {             lock (Processing)             {                 lock (WaitingQueue)         {                     return WaitingQueue.Count == 0              && Processing.Count == 0;         }             }         }     }      private readonly List<object> Processing = new List<object>();     private readonly Queue<object> WaitingQueue = new Queue<object>();      public void MethodA(object item)     {         lock (WaitingQueue)         {             if (WaitingQueue.Count > 0)             {                 if (StartItem(WaitingQueue.Peek()))                 {                     WaitingQueue.Dequeue();                 }             }         }     }      public void MethodB(object identifier)     {         lock (Processing)         {             Processing.Remove(identifier);             if (!IsDepleted)             {                 return;             }         }     //Do something...     }      bool StartItem(object item)     {         //Do something and return a value     } } 
  • 1 1 Answer
  • 3 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. 2026-05-11T02:46:51+00:00Added an answer on May 11, 2026 at 2:46 am

    It depends if you want a quick fix or a rigorous fix.

    A quick fix would be just to use one lock object in all cases.

    e.g. private readonly object _lock = new object();

    And then just lock on that. However, depending on your situation, that may impact performance more than you can accept.

    I.e. your code would become this:

    using System.Collections.Generic;  class Example {     private readonly object _lock = new object();      bool IsDepleted     {         get         {             lock (_lock)             {                 return WaitingQueue.Count == 0                  && Processing.Count == 0;             }         }     }      private readonly List<object> Processing = new List<object>();     private readonly Queue<object> WaitingQueue = new Queue<object>();      public void MethodA(object item)     {         lock (_lock)         {             if (WaitingQueue.Count > 0)             {                 if (StartItem(WaitingQueue.Peek()))                 {                     WaitingQueue.Dequeue();                 }             }         }     }      public void MethodB(object identifier)     {         lock (_lock)         {             Processing.Remove(identifier);             if (!IsDepleted)             {                 return;             }         }         //Do something...     }      bool StartItem(object item)     {         //Do something and return a value     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following class is used by another program. When it is accessed, it throws
The following class is not thread-safe (as proven in Proving the following code not
Consider the following class, with the inner struct Y being used as a type,
I inadvertently let my students overconstrain a shared class used to solve the following
Please pardon me for a n00bish question. Please consider the following code: public class
I have the following class that's used by my MVC3 application. I would like
I following Ryan's subdomain railscast where het creates a Subdomain class used in his
Can someone please explain why is meta class used in the following example. Ex:
I have the following class used for custom validation: [AttributeUsage(AttributeTargets.Property, AllowMultiple=false, Inherited=true)] public sealed
I have the following class in a Seam 2 application, which is used in

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.