【问题标题】:Would it be simple to change this code for thread synchronized queueing?更改此代码以进行线程同步排队会很简单吗?
【发布时间】:2013-02-24 17:18:10
【问题描述】:

我正在尝试运行文章Thread Synchronized Queing中的代码

但出现编译错误:

找不到类型或命名空间名称“T”(您是否缺少 使用指令还是程序集引用?)

我的猜测是它使用了泛型,虽然我没有太多经验,但更改应该是微不足道的。
我应该如何更改此代码?

我希望改变很简单,否则就忘了它

那篇文章的代码:

using System;
using System.Collections;
using System.Collections.Generic;//per comment by  @jam40jeff to answer
using System.Threading;

namespace QueueExample
{
  public class SyncQueue// per answer --> public class SyncQueue<T>
  {
    private WaitHandle[] handles = {
       new AutoResetEvent(false),
       new ManualResetEvent(false),
                                       };
    private Queue _q = new Queue();
    ////per comment by  @jam40jeff to answer, the above line should be changed to
    // private Queue<T> _q = new Queue<T>();

public int Count
{
  get
  {
    lock (_q)
    {
      return _q.Count;
    }
  }
}
public T Peek() //******error************************

{
  lock (_q)
  {
    if (_q.Count > 0)
      return _q.Peek();
  }
  return default(T);//******error************************
}

public void Enqueue(T element) //******error************************
{
  lock (_q)
  {
    _q.Enqueue(element);
    ((AutoResetEvent)handles[0]).Set();
  }
}

public T Dequeue(int timeout_milliseconds)//******error************************
{
  T element;//******error************************
  try
  {
    while (true)
    {
      if (WaitHandle.WaitAny(handles, timeout_milliseconds, true) == 0)
      {
        lock (_q)
        {
          if (_q.Count > 0)
          {
            element = _q.Dequeue();
            if (_q.Count > 0)
              ((AutoResetEvent)handles[0]).Set();
            return element;
          }
        }
      }
      else
      {
        return default(T);//******error************************
      }
    }
  }
  catch (Exception e)
  {
    return default(T);//******error************************
  }
}

public T Dequeue() //******error************************
{
  return Dequeue(-1);
}

public void Interrupt()
{
  ((ManualResetEvent)handles[1]).Set();
}
public void Uninterrupt()
{
  // for completeness, lets the queue be used again
  ((ManualResetEvent)handles[1]).Reset();
}

} }

更新:
改成后

public class SyncQueue<T> 

according to answer,也需要改成:

return _q.Peek();

return (T)_q.Peek();

element = _q.Dequeue();

element = (T)_q.Dequeue();

更新 2:
根据@jam40jeff 对答案的评论:

  • "将_q 更改为Queue&lt;T&gt; 类型。然后您将需要 using 语句,但不需要转换为 T"

我上面的更新很糟糕

【问题讨论】:

    标签: c# multithreading generics collections synchronization


    【解决方案1】:

    也许是作者的错误,SyncQueue 类应该是通用的:

    public class SyncQueue<T>
    

    而要使用泛型,您还需要再添加一个using

    using System.Collections.Generic;
    

    那么上面的代码应该没问题。

    【讨论】:

    • 我希望我纠正了其他错误,看到我的更新了吗?此外,using System.Collections.Generic; 不是必需的(使用)
    • 将 _q 更改为 Queue&lt;T&gt; 类型。然后您将需要 using 语句,但您不需要转换为 T
    猜你喜欢
    • 1970-01-01
    • 2011-05-27
    • 1970-01-01
    • 1970-01-01
    • 2015-10-22
    • 2010-10-12
    • 2019-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多