C#中的线程安全队列

时间:2022-01-22 11:46:52

I need to create a generic queue that can be queued my multiple producers, and de-queued by multiple consumers.

我需要创建一个通用队列,可以将我的多个生产者排队,并由多个消费者排队。

I want the system to at least try twice in case it attempts an operation during changes to the system, so if a process thread fails, The now flagged qBit is queued again, then when processed again, the thread performing that operation will know that another thread has already tried this operation once, and if it fails, ship this particular one off to a remediation queue for outside intervention.

我希望系统至少尝试两次,以防它在更改系统期间尝试操作,因此如果进程线程失败,则现在标记的qBit再次排队,然后再次处理时,执行该操作的线程将知道另一个线程已尝试过此操作一次,如果失败,则将此特定操作发送到补救队列以进行外部干预。

So.... to the actual question, I realize that the state of the queue could change between contains() and a queue operation, like sockets checking is just a form of error control, not a substitution. I did t this way because two producer threads may otherwise toss an error when two of the same id (identified by GUIDs) try and queue what would otherwise be different object instances of a qBit.

所以....对于实际问题,我意识到队列的状态可能在contains()和队列操作之间发生变化,就像套接字检查只是一种错误控制形式,而不是替换。我这样做是因为当两个相同的id(由GUID标识)尝试并排队qBit的不同对象实例时,两个生产者线程可能会抛出错误。

The question is, is this a reasonable implementation? I cannot see it ever becoming deadlocked because all the methods would return regardless of the result of processing a qBit, and this allows me to somewhat prevent some of those..

问题是,这是一个合理的实施吗?我无法看到它变得死锁,因为无论处理qBit的结果如何,所有方法都会返回,这使我可以在某种程度上阻止其中的一些...

Thoughts and or second set of eyes please?

思绪和第二套眼睛好吗?

public class tsQueue<t>
{
    object syncLock = new object();
    private Queue<qBit<t>> Q = new Queue<qBit<t>>();
    List<t> contents = new List<t>();

    public qBit<t> deQueue()
    {
        lock (syncLock)
        {
            qBit<t> pop =  Q.Dequeue();
            contents.Remove(pop.item);
            return pop;
        }
    }

    public void enQueue(qBit<t> push)
    {
        lock (syncLock)
        {
            contents.Add(push.item);
            Q.Enqueue(push);
        }
    }

    public bool contains(t check) {
        lock (syncLock)
        {
            return contents.Contains(check);
        }
    }

    public class qBit<t>
    {
        public bool flag { get; set; }
        private t _item;
        public t item { get { return _item; } }

        public qBit(t item)
        {
            this._item = item;
        }
    }
}

1 个解决方案

#1


0  

ConcurrentQueue is a thread safe implementation that does exactly what you need. It will also definitely be faster that your own code. Use this link to see the source code of the ConcurrentQueue implementation.

ConcurrentQueue是一个线程安全的实现,可以完全满足您的需求。你自己的代码肯定会更快。使用此链接可以查看ConcurrentQueue实现的源代码。

#1


0  

ConcurrentQueue is a thread safe implementation that does exactly what you need. It will also definitely be faster that your own code. Use this link to see the source code of the ConcurrentQueue implementation.

ConcurrentQueue是一个线程安全的实现,可以完全满足您的需求。你自己的代码肯定会更快。使用此链接可以查看ConcurrentQueue实现的源代码。