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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:42:36+00:00 2026-05-20T07:42:36+00:00

I have implemented a generic ObjectPool class but have experienced that it sometime deadlocks

  • 0

I have implemented a generic ObjectPool class but have experienced that it sometime deadlocks (happens at Monitor.Wait(poolLock))

Can anyone spot the error?

public class ObjectPool<T> where T : new()
{
    private readonly object poolLock = new object();
    Stack<T> stack = null;

    public ObjectPool(int count)
    {
        stack = new Stack<T>(count);
        for (int i=0; i<count; i++)
            stack.Push(new T());
    }

    public T Get()
    {
        lock (poolLock)
        {
            //if no more left wait for one to get Pushed
            while (stack.Count < 1)
                Monitor.Wait(poolLock); 
            return stack.Pop();
        }
    }

    public void Put(T item)
    {
        lock (poolLock)
        {
            stack.Push(item);
            //If adding first send signal
            if (stack.Count == 1)
                Monitor.Pulse(poolLock); 
        }
    }

usage

        try
        {
            service = myPool.Get();
        }
        finally
        {
            if (service != null)
                myPool.Put(service);
        }
  • 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-20T07:42:36+00:00Added an answer on May 20, 2026 at 7:42 am

    The deadlock is probably happening with a stack.Count > 0 . That means you have a Wait/Pulse problem. It is not a bad idea to always call Pulse after a Push(). Or at least when Count < 5 or so. Remember that the Wait/Pulse mechanism does not have a memory.

    A scenario:

    Thread A tries to Get from an empty
    Pool, and does a Wait()
    Thread B tries
    to Get from an empty Pool, and does a
    Wait()

    Thread C Puts into the Pool, Does a Pulse()
    Thread D Puts back into the Pool and does not Pulse (Count == 2)

    Thread A is activated and Gets its Item.
    Thread B is left Waiting. With little hope fro recovery.

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

Sidebar

Related Questions

I have a class that has everything already implemented but its initialization process is
I have a generic interface IConstrained which is implemented by the generic Constrained class.
I have a generic class that I'm trying to implement implicit type casting for.
I have seen a question here where they implemented a generic dispose method that
I want to have a generic object that implements an interface. I mean if
Looking at System.Collections.Generic.Dictionary<TKey, TValue> , it clearly implements ICollection<KeyValuePair<TKey, TValue>> , but doesn't have
I have implemented a repository pattern in my asp.net mvc web application... But i
I developed a generic Unsigned class, or really a class template Unsigned<size_t N> that
I know there are LOT of questions like that but I can't find one
I have implemented a set datatype in javascript based in a generic object type,

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.