-2
私は以下のコードを1つのプロデューサーと1つのコンシューマースレッドで持っていますが、デッドロックに入る方法はいくつかあります。私は同様の機能を達成しようとしているC#とJavaの条件付き変数が、私は周りを検索したが、それに近いものを見つけることができませんでした。この点についての助力は高く評価されます。プロデューサーのデッドロックコンシューマーC#バインドされたキュー
`
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);
}
}
}
}
}
}
`
これは単なる学習課題でない限り、独自のキューを作成することはできません。シングルスレッドの場合は、[Queue](https://msdn.microsoft.com/en-us/library/7977ey2c(v = vs.110).aspx)を使用してください。マルチスレッドプログラムの場合は、[BlockingCollection](https://msdn.microsoft.com/en-us/library/dd267312(v=vs.110).aspx)を使用してください。 –
C#で条件変数を使用する例については、http://stackoverflow.com/questions/15657637/condition-variables-c-netを参照してください。また、Stephen Toubは10年前にC#でブロックキューを行っていました。https://blogs.msdn.microsoft.com/toub/2006/04/12/blocking-queues/ –
はい、学習した内容は共有した内容が役立ちますこの点に関して – pannu