簡體   English   中英

這個無鎖的.NET隊列線程安全嗎?

[英]Is this lock-free .NET queue thread safe?

我的問題是,下面包含的類對於單讀者單編寫器隊列類線程安全嗎? 這種隊列稱為無鎖,即使隊列已填滿也會阻塞。 數據結構的靈感來自Marc Gravell在StackOverflow 上實現的阻塞隊列

結構的要點是允許單個線程將數據寫入緩沖區,而另一個線程則讀取數據。 所有這些都需要盡快發生。

Herb Sutter在DDJ文章中描述了類似的數據結構,但實現是在C ++中。 另一個區別是我使用了一個vanilla鏈表,我使用了一個鏈表的數組。

我不是僅僅包含一段代碼,而是將所有內容與允許的開源許可證(MIT許可證1.0)一起包含,以防任何人發現它有用,並且想要使用它(原樣或修改)。

這與Stack Overflow上有關如何創建阻塞並發隊列的其他問題有關(請參閱在.NET中創建blockinq隊列在.NET中創建 線程安全阻塞隊列 )。

這是代碼:

using System;
using System.Collections.Generic;
using System.Threading;
using System.Diagnostics;

namespace CollectionSandbox
{
    /// This is a single reader / singler writer buffered queue implemented
    /// with (almost) no locks. This implementation will block only if filled 
    /// up. The implementation is a linked-list of arrays.
    /// It was inspired by the desire to create a non-blocking version 
    /// of the blocking queue implementation in C# by Marc Gravell
    /// https://stackoverflow.com/questions/530211/creating-a-blocking-queuet-in-net/530228#530228
    class SimpleSharedQueue<T> : IStreamBuffer<T>
    {
        /// Used to signal things are no longer full
        ManualResetEvent canWrite = new ManualResetEvent(true);

        /// This is the size of a buffer 
        const int BUFFER_SIZE = 512;

        /// This is the maximum number of nodes. 
        const int MAX_NODE_COUNT = 100;

        /// This marks the location to write new data to.
        Cursor adder;

        /// This marks the location to read new data from.
        Cursor remover;

        /// Indicates that no more data is going to be written to the node.
        public bool completed = false;

        /// A node is an array of data items, a pointer to the next item,
        /// and in index of the number of occupied items 
        class Node
        {
            /// Where the data is stored.
            public T[] data = new T[BUFFER_SIZE];

            /// The number of data items currently stored in the node.
            public Node next;

            /// The number of data items currently stored in the node.
            public int count;

            /// Default constructor, only used for first node.
            public Node()
            {
                count = 0;
            }

            /// Only ever called by the writer to add new Nodes to the scene
            public Node(T x, Node prev)
            {
                data[0] = x;
                count = 1;

                // The previous node has to be safely updated to point to this node.
                // A reader could looking at the point, while we set it, so this should be 
                // atomic.
                Interlocked.Exchange(ref prev.next, this);
            }
        }

        /// This is used to point to a location within a single node, and can perform 
        /// reads or writers. One cursor will only ever read, and another cursor will only
        /// ever write.
        class Cursor
        {
            /// Points to the parent Queue
            public SimpleSharedQueue<T> q;

            /// The current node
            public Node node;

            /// For a writer, this points to the position that the next item will be written to.
            /// For a reader, this points to the position that the next item will be read from.
            public int current = 0;

            /// Creates a new cursor, pointing to the node
            public Cursor(SimpleSharedQueue<T> q, Node node)
            {
                this.q = q;
                this.node = node;
            }

            /// Used to push more data onto the queue
            public void Write(T x)
            {
                Trace.Assert(current == node.count);

                // Check whether we are at the node limit, and are going to need to allocate a new buffer.
                if (current == BUFFER_SIZE)
                {
                    // Check if the queue is full
                    if (q.IsFull())
                    {
                        // Signal the canWrite event to false
                        q.canWrite.Reset();

                        // Wait until the canWrite event is signaled 
                        q.canWrite.WaitOne();
                    }

                    // create a new node
                    node = new Node(x, node);
                    current = 1;
                }
                else
                {
                    // If the implementation is correct then the reader will never try to access this 
                    // array location while we set it. This is because of the invariant that 
                    // if reader and writer are at the same node: 
                    //    reader.current < node.count 
                    // and 
                    //    writer.current = node.count 
                    node.data[current++] = x;

                    // We have to use interlocked, to assure that we incremeent the count 
                    // atomicalluy, because the reader could be reading it.
                    Interlocked.Increment(ref node.count);
                }
            }

            /// Pulls data from the queue, returns false only if 
            /// there 
            public bool Read(ref T x)
            {
                while (true)
                {
                    if (current < node.count)
                    {
                        x = node.data[current++];
                        return true;
                    }
                    else if ((current == BUFFER_SIZE) && (node.next != null))
                    {
                        // Move the current node to the next one.
                        // We know it is safe to do so.
                        // The old node will have no more references to it it 
                        // and will be deleted by the garbage collector.
                        node = node.next;

                        // If there is a writer thread waiting on the Queue,
                        // then release it.
                        // Conceptually there is a "if (q.IsFull)", but we can't place it 
                        // because that would lead to a Race condition.
                        q.canWrite.Set();

                        // point to the first spot                
                        current = 0;

                        // One of the invariants is that every node created after the first,
                        // will have at least one item. So the following call is safe
                        x = node.data[current++];
                        return true;
                    }

                    // If we get here, we have read the most recently added data.
                    // We then check to see if the writer has finished producing data.
                    if (q.completed)
                        return false;

                    // If we get here there is no data waiting, and no flagging of the completed thread.
                    // Wait a millisecond. The system will also context switch. 
                    // This will allow the writing thread some additional resources to pump out 
                    // more data (especially if it iself is multithreaded)
                    Thread.Sleep(1);
                }
            }
        }

        /// Returns the number of nodes currently used.
        private int NodeCount
        {
            get
            {
                int result = 0;
                Node cur = null;
                Interlocked.Exchange<Node>(ref cur, remover.node);

                // Counts all nodes from the remover to the adder
                // Not efficient, but this is not called often. 
                while (cur != null)
                {
                    ++result;
                    Interlocked.Exchange<Node>(ref cur, cur.next);
                }
                return result;
            }
        }

        /// Construct the queue.
        public SimpleSharedQueue()
        {
            Node root = new Node();
            adder = new Cursor(this, root);
            remover = new Cursor(this, root);
        }

        /// Indicate to the reader that no more data is going to be written.
        public void MarkCompleted()
        {
            completed = true;
        }

        /// Read the next piece of data. Returns false if there is no more data. 
        public bool Read(ref T x)
        {
            return remover.Read(ref x);
        }

        /// Writes more data.
        public void Write(T x)
        {
            adder.Write(x);
        }

        /// Tells us if there are too many nodes, and can't add anymore.
        private bool IsFull()
        {
            return NodeCount == MAX_NODE_COUNT;  
        }
    }
}

Microsoft Research CHESS應該被證明是一個測試實現的好工具。

Sleep()的存在使得無鎖方法完全沒用。 面對無鎖設計復雜性的唯一理由是需要絕對速度並避免信號量的成本。 睡眠(1)的使用完全擊敗了這個目的。

鑒於我找不到Interlocked.Exchange執行讀取或寫入塊的任何引用,我會說不。 我也會質疑為什么你想要無鎖,因為很少有足夠的好處來抵消它的復雜性。

微軟在2009年的GDC上有一個很好的演示,你可以在這里獲得幻燈片。

注意雙重檢查 - 單鎖模式(如上面引用的鏈接: http//www.yoda.arachsys.com/csharp/singleton.html

從Andrei Alexandrescu的“現代C ++設計”中逐字引用

我懷疑它不是線程安全的 - 想象一下以下場景:

兩個線程進入cursor.Write 第一個獲取到line node = new Node(x, node); if (current == BUFFER_SIZE)語句的真正一半中(但是我們也假設current == BUFFER_SIZE )所以當1被添加到current另一個進入的線程將通過if語句跟隨另一個路徑。 現在想象一下,線程1失去了它的時間片,線程2得到它,然后繼續輸入if語句,錯誤地認為條件仍然存在。 它應該進入另一條路徑。

我也沒有運行這個代碼,所以我不確定我的假設在這段代碼中是否可行,但是如果它們是(即當current == BUFFER_SIZE時從多個線程進入current == BUFFER_SIZE ),那么它可能很容易發生並發錯誤。

首先,我想知道這兩行順序代碼的假設:

                node.data[current++] = x;

                // We have to use interlocked, to assure that we incremeent the count 
                // atomicalluy, because the reader could be reading it.
                Interlocked.Increment(ref node.count);

可以說node.data []的新值已經提交到這個內存位置了嗎? 它不存儲在易失性存儲器地址中,因此如果我理解正確,它可以被緩存嗎? 這不會導致'臟'讀? 可能還有其他地方也是如此,但這一個地方一目了然。

第二,包含以下內容的多線程代碼:

Thread.Sleep(int);

......永遠不是一個好兆頭。 如果需要,那么代碼注定要失敗,如果不需要它就是浪費。 我真的希望他們完全刪除這個API。 意識到這是一個等待至少那段時間的請求。 隨着上下文切換的開銷,你幾乎肯定會等待更長時間。

第三,我完全不了解Interlock API在這里的使用。 也許我累了,只是錯過了重點; 但我找不到兩個線程讀取和寫入同一個變量的潛在線程沖突? 似乎我能找到的互鎖交換的唯一用途是修改node.data []的內容以修復上面的#1。

最后,似乎實施有點過於復雜。 我錯過了整個Cursor / Node的觀點,還是基本上和這個類做同樣的事情? (注意:我沒有嘗試過,我也不認為這也是線程安全的,只是試圖歸結我認為你在做什么。)

class ReaderWriterQueue<T>
{
    readonly AutoResetEvent _readComplete;
    readonly T[] _buffer;
    readonly int _maxBuffer;
    int _readerPos, _writerPos;

    public ReaderWriterQueue(int maxBuffer)
    {
        _readComplete = new AutoResetEvent(true);
        _maxBuffer = maxBuffer;
        _buffer = new T[_maxBuffer];
        _readerPos = _writerPos = 0;
    }

    public int Next(int current) { return ++current == _maxBuffer ? 0 : current; }

    public bool Read(ref T item)
    {
        if (_readerPos != _writerPos)
        {
            item = _buffer[_readerPos];
            _readerPos = Next(_readerPos);
            return true;
        }
        else
            return false;
    }

    public void Write(T item)
    {
        int next = Next(_writerPos);

        while (next == _readerPos)
            _readComplete.WaitOne();

        _buffer[next] = item;
        _writerPos = next;
    }
}

所以我完全偏離了這里,並沒有看到原始課程中的魔力?

我必須承認一件事,我鄙視線程。 我見過最好的開發者失敗了。 本文給出了一個很好的例子,說明如何正確處理線程: http://www.yoda.arachsys.com/csharp/singleton.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM