I have the following code with single producer and a single consumer thread but the they some how get into dead lock. I am trying to achieve similar functionality if java conditional variable with C# but I have searched around but didn't find any thing close to it. Any help in this regard would be highly appreciated.
`
private List<T> coffeeBevrages;
private volatile int count;
private int max;
private int consumed = 0;
static Semaphore pool;
public Queue()
{
max = 10;
pool = new Semaphore(0, max);
count = 0;
coffeeBevrages = new List<T>();
}
public void busyAdd(T name)
{
while (!add(name)) Console.WriteLine("producesr busy");
}
public void busyRemove(T name)
{
while (!remove(name)) Console.WriteLine("consumer busy");
}
private bool add(T name)
{
lock(this)
{
if (count < max)
{
count++;
coffeeBevrages.Add(name);
return true;
}
else
return false;
}
}
private bool remove(T name)
{
lock (this)
{
if (count > 0)
{
count--;
Console.WriteLine(coffeeBevrages.Remove(name));
consumed++;
Console.WriteLine(consumed);
return true;
}
else
return false;
}
}
public void sleepAdd(T name)
{
Console.WriteLine("Hey......################");
#region locking code
lock (this)
{
if (count < max)
{
count++;
consumed++;
Console.WriteLine("Produced : " + consumed);
Console.WriteLine("Here notification p " + count);
coffeeBevrages.Add(name);
Monitor.PulseAll(this);
}
else
{
while (count == max)
{
Console.WriteLine("Here " + count);
Monitor.Wait(this,100);
}
}
#endregion
}
}
public void sleepremove(T name)
{
lock (this)
{
if (count > 0)
{
Console.WriteLine("Here notification c " + count);
count--;
Monitor.PulseAll(this);
}
else
{
while (count == 0)
{
Console.WriteLine("Here" + count);
Monitor.Wait(this,100);
}
}
}
}
}
}
`