这是使用一个静态队列的线程安全的?这是、队列、线程、静态

2023-09-03 00:27:22 作者:花哭了那一夏

MSDN文档指出静态通用队列是线程安全的。这是否意味着以下code是线程安全的?换句话说,就是有一个问题,当一个线程入队一个int而另一个线程队列中取出一个int在同一时间?我一定要锁入队和出队操作的线程安全?

The msdn documentation states that a static generic Queue is thread-safe. Does this mean that the following code is thread-safe? In other words, is there a problem when a thread Enqueues an int and another thread Dequeues an int at the same time? Do I have to lock the Enqueue and Dequeue operations for thread-safety?

class Test {
    public static Queue<int> queue = new Queue<int>(10000);

    Thread putIntThread;
    Thread takeIntThread;

    public Test() {
        for(int i = 0; i < 5000; ++i) {
            queue.Enqueue(0);
        }
        putIntThread = new Thread(this.PutInt);
        takeIntThread = new Thread(this.TakeInt);
        putIntThread.Start();
        takeIntThread.Start();
    }

    void PutInt() {
        while(true)
        {
            if(queue.Count < 10000) {//no need to lock here as only itself can change this condition
                queue.Enqueue(0);
            }
        }
    }

    void TakeInt() {
        while(true) {
            if(queue.Count > 0) {//no need to lock here as only itself can change this condition
                queue.Dequeue();
            }
        }
    }

}

编辑:我必须使用.NET 3.5

I have to use .NET 3.5

推荐答案

这是绝对的没有的是线程安全的。从 问答其中的文档; T&GT;

This is absolutely not thread-safe. From the docs of Queue<T>.

公共静态这种类型的成员(在Visual Basic中的Shared)都是线程安全的。所有实例成员不能保证是线程安全的。

Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

A 问答LT; T&GT; 可以同时支持多个阅读,只要集合不会被改动。即便如此,通过集合进行枚举本质上并不是一个线程安全的过程。若要确保枚举过程中的线程安全,可以在整个枚举过程中锁定集合。要允许多个线程读取和写入访问的集合,您必须实现自己的同步。

A Queue<T> can support multiple readers concurrently, as long as the collection is not modified. Even so, enumerating through a collection is intrinsically not a thread-safe procedure. To guarantee thread safety during enumeration, you can lock the collection during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.

重读你的问题,你似乎混淆了那句这种类型的静态成员 - 这不是在谈论静态队列,因为没有这样的事情。一个对象不是静态的或不 - 一个成员。当谈到静态成员,它在谈论的东西像 Encoding.GetEncoding 问答LT; T&GT; 实际上并没有任何静态成员)。实例成员都是像排队出列 - 其中涉及到的类型,而不是类型本身的实例成员。

Rereading your question, you seem to be confused about the phrase "static members of this type" - it's not talking about "a static Queue" as there's no such thing. An object isn't static or not - a member is. When it talks about static members it's talking about things like Encoding.GetEncoding (Queue<T> doesn't actually have any static members). Instance members are things like Enqueue and Dequeue - members which relate to an instance of the type rather than the type itself.

因此​​,无论你需要为每个操作使用锁定,或者如果你使用.NET 4中,使用ConcurrentQueue<T>.

So either you need to use a lock for each action, or if you're using .NET 4, use ConcurrentQueue<T>.