发出信号后,处置自身

发布于 2025-01-25 11:34:06 字数 1269 浏览 3 评论 0 原文

我们假设我们有一个类 myqueue 定义为以下:

public class MyQueue
{
    private bool _stop;
    private readonly AutoResetEvent _onQueued = new AutoResetEvent(false);
    private readonly ConcurrentQueue<int> _queue = new ConcurrentQueue<int>();
    
    public MyQueue()
    {
        var thread = new Thread(ManageQueue)
        {
            IsBackground = true
        };
        thread.Start();
    }

    public void EnqueueInt(int num)
    {
         _queue.Enqueue(num);
         _onQueued.Set();
    }

    public void Stop()
    {
       _stop = true;
       _onQueued.Set();
    }

    private void ManageQueue()
    {
        while (!_stop)
        {
            onQueued.WaitOne();
            int tempNum;

            while(!_stop && _queue.TryDequeue(out tempNum))
            {
                 // Do something with the int
            }
        }
    }
}

因此,我们有一个方法 enqueInt 用于在数据结构内排队项目和 thread> thread 该周期性检查队列是否有要处理的项目。信号 _onqueed 用于在插入新项目或启动关闭操作时发出信号。我的问题是:Myqueue具有一次性对象( autoresetevent ),必须在不再需要队列时处置,但是,处置它的最佳方法是什么? 呼叫 distose 关闭()操作之后可能非常危险,因为其中 set() invocation,我没有Garantee认为线程已终止其周期。

Let's suppose we have a class MyQueue defined as below:

public class MyQueue
{
    private bool _stop;
    private readonly AutoResetEvent _onQueued = new AutoResetEvent(false);
    private readonly ConcurrentQueue<int> _queue = new ConcurrentQueue<int>();
    
    public MyQueue()
    {
        var thread = new Thread(ManageQueue)
        {
            IsBackground = true
        };
        thread.Start();
    }

    public void EnqueueInt(int num)
    {
         _queue.Enqueue(num);
         _onQueued.Set();
    }

    public void Stop()
    {
       _stop = true;
       _onQueued.Set();
    }

    private void ManageQueue()
    {
        while (!_stop)
        {
            onQueued.WaitOne();
            int tempNum;

            while(!_stop && _queue.TryDequeue(out tempNum))
            {
                 // Do something with the int
            }
        }
    }
}

So pratically we have a method EnqueueInt used to queue item inside the data structure and a thread that cyclically checks if the queue has item to be processed. The signal _onQueued is used to signal the thread when a new item is inserted or a close operation has been started. My question is: MyQueue has a disposable object (of type AutoResetEvent) and must be disposed when the queue is no longer needed, but, what's the best way to dispose it?
Calling Dispose after a Close() operation could be very dangerous because of the Set() invocation in it and I've no garantee that the Thread has terminated its cycle.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的绿洲 2025-02-01 11:34:06

相反,或重新启动轮子,您可以使用 system.collections.generic.queue&lt; t&gt;

另外,如果您要管理队列的寿命,我将使用服务,因此,当服务(queue服务)被处置时,那么您可以处置它;这是有关如何创建队列服务的Microsoft指南:

Instead or re-inventing the wheel, you can use System.Collections.Generic.Queue<T>

Also, if you want to manage the lifetime of your queue, I would use a service, so when the service (queue service) is disposed, then you can dispose of it; here is the Microsoft guide on how to create a queue service: https://learn.microsoft.com/en-us/dotnet/core/extensions/queue-service

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文