是简单的更改此代码为线程同步队列吗? [英] Would it be simple to change this code for thread synchronized queueing?

查看:98
本文介绍了是简单的更改此代码为线程同步队列吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从文章执行线程同步排队

但出现编译错误:


类型或命名空间名称无法找到'T'(您是否缺少一个
使用指令或程序集引用?)

The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)

它使用泛型,并且更改应该是相当微不足道,虽然我没有太多经验。

我应该如何更改此代码?

My guess is that it is using generics and the change should be pretty trivial though I have not much experience with it.
How should I change this code?

我希望这个简单的改动,否则只是忘了它。

I hope for the pretty simple change, otherwise just forget about it

/ p>

The code from that article:

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();
}

}
}

更新:

更改为

Update:
After changing to

public class SyncQueue<T> 

要回答,还需要改变:

according to answer, it was also necessary to change from:

return _q.Peek();

return (T)_q.Peek();

element = _q.Dequeue();

element = (T)_q.Dequeue();

Update2:

根据@ jam40jeff的评论到答案:

Update2:
Per comment of @jam40jeff to the answer:


  • _q 更改为 Queue< T> 。然后你将需要using语句,但是你不需要转换为T

  • "Change _q to be of type Queue<T>. Then you will need the using statement, but you won't need the casts to T"

我的* 更新 *以上是不好的

my *update*s above were bad

推荐答案

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

Maybe it's the mistake from author, class SyncQueue should be generic:

public class SyncQueue<T>

要使用泛型,还需要使用 c $ c>:

And to use generic, you also add one more using:

using System.Collections.Generic;

然后上面的代码就可以了。

Then the code above should be fine.

这篇关于是简单的更改此代码为线程同步队列吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆